Part Number Hot Search : 
CY7C195B BYT54A SBC5461 TCR2BE12 BYT56G UF4004 BZX84C16 OP123
Product Description
Full Text Search
 

To Download STC12LE5204 Datasheet File

  If you can't view the Datasheet, Please click here to try to view without PDF Reader .  
 
 


  Datasheet File OCR Text:
  mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 1 www.stcmcu.com stc12c5a60s2 series mcu stc12le5a60s2 series mcu data sheet stc mcu limited www.stcmcu.com update date: 2011-7-15 stc mcu limited. free datasheet http:///
contents chapter 1. introduction ................................................................. 8 1.1 features .................................................................................................. 8 1.2 block diagram ........................................................................................ 9 1.3 pin configurations ............................................................................... 1 0 1.4 stc12c5a60s2 series selection table ............................................... 1 3 1.5 stc12c5a60s2 series minimum application system ....................... 1 6 1.6 stc12c5a60s2 series application circuit for isp ............................. 1 8 1.7 pin descriptions ................................................................................... 2 0 1.8 package dimension drawings .............................................................. 2 1 1.9 stc12c5a60s2 series mcu naming rules ......................................... 2 6 1.10 global unique identification number (id) ........................................ 2 7 chapter 2. clock, power management and reset .................... 3 0 2.1 clock .................................................................................................... 3 0 2.1.1 on-chip r/c clock and external crystal/clcok are optional in stc-isp.exe . 3 0 2.1.2 divider for system clock ................................................................................... 3 1 2.1.3 how to know internal rc oscillator frequency(internal clock frequency) ...... 3 2 2.1.4 programmable clock output ............................................................................... 3 5 2.1.4.1 timer 0 programmable clock-out on p3.4 ......................................................................... 3 7 2.1.4.2 timer 1 programmable clock-out on p3.5 ......................................................................... 3 8 2.1.4.3 baud rate generator and programmable clock output on p1.0 ....................................... 3 8 2.2 power management modes .................................................................. 3 9 2.2.1 slow down mode ................................................................................................ 4 0 2.2.2 idle mode ............................................................................................................. 4 1 2.2.3 stop / power down (pd) mode ........................................................................... 4 2 2.3 reset sources .................................................................................... 4 8 2.3.2 external low voltage detection reset (high reliable reset, rst2 pin reset) ... 4 9 2.3.3 external low voltage detection if not be used rst2 can be as interrupt .......... 5 0 2.3.4 software reset .................................................................................................. 5 4 2.3.5 power-on reset (por) ........................................................................................ 5 4 2.3.5 max810 power-on-reset delay .......................................................................... 5 5 2.3.3 watch-dog-timer ................................................................................................ 5 6 2.3.8 warm boot and cold boot reset ......................................................................... 6 0 stc mcu limited free datasheet http:///
chapter 3. memory organization .............................................. 6 1 3.1 program memory ................................................................................. 6 1 3.2 data memory ........................................................................................ 6 2 3.2.1 on-chip scratch-pad ram .................................................................................. 6 2 3.2.2 auxiliary ram .................................................................................................... 6 4 3.2.3 external expandable 64kb ram (off-chip ram) ........................................... 7 0 3.3 special function registers ................................................................... 7 3 3.3.1 special function registers address map ............................................................ 7 3 3.3.2 special function registers bits description ....................................................... 7 4 3.3.3 dual data pointer register (dptr) .................................................................... 7 8 chapter 4. configurable i/o ports of stc12c5axx series ..... 8 0 4.1 i/o ports configurations ...................................................................... 8 0 4.2 p4/p5 of stc12c5a60s2 series .......................................................... 8 5 4.3 i/o ports modes .................................................................................... 8 7 4.3.1 quasi-bidirectional i/o ........................................................................................ 8 7 4.3.2 push-pull output .................................................................................................. 8 8 4.3.3 input-only (high-impedance)mode ..................................................................... 8 8 4.3.4 open-drain output ............................................................................................... 8 8 4.4 i/o port application notes ..................................................................... 8 9 4.5 typical transistor control circuit ........................................................... 8 9 4.6 typical diode control circuit ................................................................. 8 9 4.7 3v/5v hybrid system ............................................................................ 9 0 4.8 how to make i/o port low after mcu reset ......................................... 9 1 4.9 i/o status while pwm outputing .......................................................... 9 1 4.10 i/o drive led application circuit ....................................................... 9 2 4.11 i/o immediately drive lcd application circuit .................................. 9 3 4.12 using a/d conversion to scan key application circuit ...................... 9 4 chapter 5. instruction system .................................................... 9 5 5.1 addressing modes ................................................................................ 9 5 5.2 instruction set summary ...................................................................... 9 6 5.3 instruction definitions ........................................................................ 10 1 chapter 6. interrupt system ..................................................... 13 8 6.1 interrupt structure .............................................................................. 14 0 6.2 interrupt register ................................................................................ 14 2 stc mcu limited free datasheet http:///
6.3 interrupt priorities .............................................................................. 15 3 6.4 how interrupts are handled .............................................................. 15 4 6.5 external interrupts ............................................................................. 15 5 6.6 response time .................................................................................. 15 9 6.7 demo programs about interrupts (c and assembly programs) ......... 16 0 6.7.1 external interrupt 0 ( int0 ) demo programs (c and asm) ............................ 16 0 6.7.2 external interrupt 1 ( int1 ) demo programs (c and asm) ............................ 16 4 6.7.3 programs of p3.4/t0/ int interrupt(falling edge) used to wake up pd mode .. 16 8 6.7.4 programs of p3.5/t1/ int interrupt(falling edge) used to wake up pd mode .. 17 0 6.7.5 program of p3.0/rxd/ int interrupt(falling edge) used to wake up pd mode . 17 2 6.7.6 demo program of low voltage detection interrupt used to wake up pd mode 17 5 6.7.7 program of pca interrupt used to wake up power down mode ....................... 17 8 chapter 7. timer/counter 0/1 .................................................. 18 2 7.1 special function registers about timer/counter ............................... 18 2 7.2 timer/counter 0 mode of operation (compatible with traditional 8051 mcu) ......... 18 7 7.2.1 mode 0 (13-bit timer/counter) ...................................................................................... 18 7 7.2.2 mode 1 (16-bit timer/counter) and demo programs (c and asm) .............................. 18 8 7.2.3 mode 2 (8-bit auto-reload mode) and demo programs (c and assembly program) ... 19 2 7.2.4 mode 3 (two 8-bit timers/couters) ............................................................................... 19 5 7.3 timer/counter 1 mode of operation .................................................. 19 6 7.3.1 mode 0 (13-bit timer/counter) ...................................................................................... 19 6 7.3.2 mode 1 (16-bit timer/counter) and demo programs (c and asm) .............................. 19 7 7.3.3 mode 2 (8-bit auto-reload mode) and demo programs (c and asm) ......................... 20 1 7.4 programmable clock output and demo programs (c and asm) ..... 20 4 7.4.1 timer 0 programmable clock-out on p3.4 and demo program ........................ 20 6 7.4.2 timer 1 programmable clock-out on p3.5 and demo program ........................ 20 9 7.4.3 baud rate generator programmable clock-out on p1.0 and demo program .. 21 2 7.5 application notes for timer in practice ............................................. 21 9 chapter 8. serial interface (uart) ........................................ 22 0 8.1 uart with enhanced function ........................................................... 22 0 8.1.1 special function registers about uart1 ......................................................... 22 0 8.1.2 uart1 operation modes ................................................................................. 22 5 8.1.2.1 mode 0: 8-bit shift register ............................................................................................ 22 5 8.1.2.2 mode 1: 8-bit uart with variable baud rate ................................................................ 22 7 8.1.2.3 mode 2: 9-bit uart with fixed baud rate .................................................................... 22 9 8.1.2.4 mode3: 9-bit uart with variable baud rate ................................................................. 23 1 stc mcu limited free datasheet http:///
8.1.3 frame error detection ....................................................................................... 23 3 8.1.4 multiprocessor communications ....................................................................... 23 3 8.1.5 automatic address recognition ........................................................................ 23 4 8.1.6 buad rates and demo program ......................................................................... 23 6 8.1.7 demo programs about uart1 (c and asm) ................................................... 24 0 8.2 secondary uart (s2) ........................................................................ 24 6 8.2.1 special function registers about s2 (uart2) ................................................. 24 6 8.2.3 uart2 operation modes .................................................................................. 25 0 8.2.3.1 mode 0: 8-bit shift register ............................................................................................. 25 0 8.2.3.2 mode 1: 8-bit uart2 with variable baud-rate .............................................................. 25 0 8.2.3.3 mode 2: 9-bit uart2 with fixed baud-rate .................................................................. 25 0 8.2.3.4 mode 3: 9-bit uart2 with variable baud-rate .............................................................. 25 0 8.2.4 demo program about secondary uart ........................................................... 25 1 chapter 9. analog to digital converter ................................... 26 7 9.1 a/d converter structure ..................................................................... 26 7 9.2 registers for adc .............................................................................. 26 9 9.3 application circuit of a/d converter ............................................... 27 5 9.4 adc application circuit for key scan .............................................. 27 6 9.5 a/d reference voltage source ............................................................. 27 7 9.6 program using interrupts to demostrate a/d conversion ................. 27 8 9.7 program using polling to demostrate a/d conversion ..................... 28 4 chapter 10. programmable counter array(pca) ................. 29 0 10.2 sfrs related with pca ..................................................................... 29 0 10.2 pca/pwm structure ........................................................................ 29 6 10.3 pca modules operation mode ....................................................... 29 8 10.3.1 pca capture mode .......................................................................................... 29 8 10.3.2 16-bit software timer mode ........................................................................... 29 9 10.3.3 high speed output mode ................................................................................ 30 0 10.3.4 pulse width modulator mode (pwm mode) ................................................... 30 1 10.4 programs for pca module extended external interrupt .................. 30 2 10.5 demo programs for pca module acted as 16-bit timer .................. 30 5 10.6 programs for pca module as 16-bit high speed output ................. 30 9 10.7 demo programs for pca module as pwm output (c and asm) ... 31 3 10.8 demo program for pca clock base on timer 1 overflow rate ........ 31 6 10.9 using pwm achieve d/a conversion function reference circuit .... 32 0 stc mcu limited free datasheet http:///
chapter 11. serial peripheral interface (spi) ......................... 32 1 11.1 special function registers related with spi ..................................... 32 1 11.2 spi structure ..................................................................................... 32 5 11.3 spi data communication ................................................................. 32 6 11.3.1 spi configuration ............................................................................................ 32 6 11.3.2 spi data communication modes .................................................................... 32 7 11.3.3 spi data modes ............................................................................................... 32 9 11.4 spi function demo programs (single master single slave) ...... 33 1 11.4.1 spi function demo programs using interrupts (c and asm) ......................... 33 1 11.4.2 spi function demo programs using polling (c and asm) ............................. 33 7 11.5 spi function demo programs (each other as the master-slave) ..... 34 3 11.5.1 spi function demo programs using interrupts (c and asm) ......................... 34 3 11.5.2 spi function demo programs using polling ................................................... 34 9 11.6 spi demo (single master multiple slave) ...................................... 35 5 chapter 12. iap / eeprom ..................................................... 36 5 12.1 iap / eeprom special function registers ..................................... 36 6 12.2 stc12c5a60s2 series internal eeprom allocation table ........... 36 9 12.3 iap/eeprom assembly language program introduction ............. 37 1 12.4 eeprom demo program (c and asm) .......................................... 37 4 chapter 13. stc12 series programming tools usage .............. 38 2 13.1 in-system-programming (isp) principle .......................................... 38 2 13.2 stc12c5a60s2 series application circuit for isp ........................... 38 3 13.3 pc side application usage ................................................................. 38 5 13.4 compiler / assembler programmer and emulator ........................... 38 7 13.5 self-defined isp download demo .................................................. 38 7 appendix a: assembly language programming .................... 39 1 appendix b: 8051 c programming .......................................... 41 3 appendix c: stc12c5axx series electrical characteristics 42 3 appendix d: program for indirect addressing inner 256b ram ............................................................................... 42 5 appendix e: using serial port expand i/o interface ............. 42 6 free datasheet http:///
appendix f: use stc mcu common i/o driving lcd display ............................................................................... 42 8 appendix g: led driven by an i/o port and key scan ......... 43 5 appendix h: how to reduce the length of code using keil c .... ............................................................................... 43 6 appendix i: notes of stc12 series replaced traditional 8051 ... ............................................................................... 43 7 free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 8 www.stcmcu.com 1.1 features enhanced 80c51 central processing unit ,1t per machine cycle, faster 6~7 times than the rate of a standard 8051. operating voltage range: 5.5v ~ 3.5v or 2.2v ~ 3.6v (stc12le5a60s2). operating frequency range: 0- 35mhz, is equivalent to standard 8051:0~420mhz on-chip 8/16/20/32/40/48/52/56/60/62k flash program memory with flexible isp/iap capability on-chip 1280 byte ram: 256 byte scratch-pad ram and 1024 bytes of auxiliary ram be capable of addressing up to 64k byte of external ram dual data pointer (dptr) to speed up data movement code protection for flash memory access excellent noise immunity, very low power consumption four 16-bit timer/counter, be compatible with timer0/timer1 of standard 8051, 2-channel pca can be available as two timers. 10 vector-address, 4 level priority interrupt capability one enhanced uart with hardware address-recognition and frame-error detection function secondary uart with self baud-rate generator one 15 bits watch-dog-timer with 8-bit pre-scaler (one-time-enabled) spi master/slave communication interface two channel programmable counter array (pca) 10-bit, 8-channel analog-to-digital converter (adc) simple internal rc oscillator and external crystal clock power control: idle mode(all interrupt can wake up idle mode) , power-down mode(external interrupt can wake up power-down mode) and slow down mode power down mode can be woken-up by pca_pin, rxd_pin, t0/t1 pin and external interrupts (int0, int1) 44/40/36 programmable i/o ports are available programmable clock output function. t0 output the clock on p3.4,t1 output the clock on p3.5,brt output the clock on p1.0 external low-voltage detector function(p4.6, the ea pin at the pin location of standard 8051) five package type : lqfp-44, lqfp-48 ,pdip-40, plcc-44,qfn-40 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? chapter 1. introduction stc12c5a60s2 is a single-chip microcontroller based on a high performance 1t architecture 80c51 cpu, which is produced by stc mcu limited. with the enhanced kernel, stc12c5a60s2 executes instructions in 1~6 clock cycles (about 6~7 times the rate of a standard 8051 device), and has a fully compatible instruction set with industrial-standard 80c51 series microcontroller. in-system-programming (isp) and in-application- programming (iap) support the users to upgrade the program and data in system. isp allows the user to download new code without removing the microcontroller from the actual end product; iap means that the device can write non-valatile data in flash memory while the application program is running. the stc12c5a60s2 retains all fea - tures of the standard 80c51. in addition, the stc12c5a60s2 has two extra i/o ports (p4 and p5), a 10-sources, 4-priority-level interrupt structure, 10-bit adc, two uarts, on-chip crystal oscillator , a 2-channel pca and pwm, spi, a one-time enabled watchdog timer. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 9 www.stcmcu.com 1.2 block diagram the cpu kernel of stc12c5a60s2 is fully compatible to the standard 8051 microcontroller, maintains all instruction mnemonics and binary compatibility. with some great architecture enhancements, stc12c5a60s2 executes the fastest instructions per clock cycle. improvement of individual programs depends on the actual instructions used. stc12c5a60s2 block diagram ram 256b ram addr register flash 64k program counter pca spi b register acc tmp2 tmp1 stack pointer alu psw wdt control unit xtal2 xtal1 reset aux-ram 1024b isp/iap address generator timer 0/1 enhanced uart uart2 (s2) lvd/lvr port 0,2,3,4,5 latch port 0,2,3,4,5 driver p0,p2,p3,p4,p5 port1 latch port 1 driver p1.0 ~ p1.7 adc p1.0 ~ p1.7 8 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 10 www.stcmcu.com 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 vcc ale/p4.5 na/p4.4 p4.7/rst txd/p3.1 xtal2 xtal1 gnd wr/p3.6 rd/p3.7 int/rxd/p3.0 clkout0/int/t0/p3.4 clkout1/int/t1/p3.5 int1/p3.3 int0/p3.2 p0.0 p0.1 p0.2 p0.3 p0.4 p0.5 p0.6 p0.7 ex_lvd/p4.6/rst2 p2.7/a15 p2.6/a14 p2.5/a13 p2.4/a12 p2.3/a11 p2.2/a10 p2.1/a9 p2.0/a8 clkout2/adc0/p1.0 adc1/p1.1 rxd2/eci/adc2/p1.2 txd2/cpp0/adc3/p1.3 ss/cpp1/adc4/p1.4 mosi/adc5/p1.5 miso/adc6/p1.6 sclk/adc7/p1.7 33 32 31 30 29 28 27 26 25 24 23 1 2 3 4 5 6 7 8 9 10 11 p4.7/rst txd/p3.1 int/rxd/p3.0 int0/p3.2 int1/p3.3 clkout0/int/t0/p3.4 clkout1/int/t1/p3.5 ale/p4.5 na/p4.4 vcc xtal2 xtal1 gnd p3.6/wr p3.7/rd p0.4 p0.5 p0.6 ex_lvd/p4.6/rst2 p4.1/eci/mosi p2.7/a15 p2.6/a14 p2.5/a13 p0.7 mosi/adc5/p1.5 miso/adc6/p1.6 sclk/adc7/p1.7 sclk/txd2/ccp1/p4.3 ss/ccp1/adc4/p1.4 txd2/ccp0/adc3/p1.3 rxd2/eci/adc2/p1.2 adc1/p1.1 clkout2/adc0/p1.0 p0.0 p0.1 p0.2 p0.3 miso/rxd2/ccp0/p4.2 p4.0/ss p2.0/a8 p2.1/a9 p2.2/a10 p2.3/a11 p2.4/a12 p0.4 p0.5 p0.6 p0.7 ex_lvd/p4.6/rst2 p4.1/eci/mosi ale/p4.5 na/p4.4 p2.7/a15 p2.6/a14 p2.5/a13 p5.1 p5.3 mosi/adc5/p1.5 miso/adc6/p1.6 sclk/adc7/p1.7 p4.7/rst sclk/txd2/ccp1/p4.3 txd/p3.1 p5.2 ss/ccp1/adc4/p1.4 txd2/ccp0/adc3/p1.3 rxd2/eci/adc2/p1.2 adc1/p1.1 adc0/clkout2/p1.0 miso/rxd2/ccp0/p4.2 vcc p0.0 p0.1 p0.2 p0.3 p5.0 1 2 3 4 5 6 7 8 9 10 11 12 36 35 34 33 32 31 30 29 28 27 26 25 int/rxd/p3.0 int0/p3.2 int1/p3.3 clkout0/int/t0/p3.4 clkout1/int/t1/p3.5 lqfp-48 stc12c5a60s2 pdip-40 stc12c5a60s2 lqfp-44 stc12c5a60s2 1.3 pin configurations 34 35 36 37 38 39 40 41 42 43 44 22 21 20 19 18 17 16 15 14 13 12 37 38 39 40 41 42 43 44 45 46 47 48 24 23 22 21 10 19 18 17 16 15 14 13 xtal2 xtal1 gnd p3.6/wr p3.7/rd p4.0/ss p2.0/a8 p2.1/a9 p2.2/a10 p2.3/a11 p2.4/a12 ccp is an abbreviation of capture, compare and pwm stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 11 www.stcmcu.com plcc-44 stc12c5a60s2 39 38 37 36 35 34 33 32 31 30 29 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 6 5 4 3 2 1 44 43 42 41 40 ale/p4.5 na/p4.4 p0.4 p0.5 p0.6 ex_lvd/p4.6/rst2 p4.1/eci/mosi p2.7/a15 p2.6/a14 p2.5/a13 p0.7 xtal2 xtal1 gnd wr/p3.6 rd/p3.7 ss/p4.0 a8/p2.0 a9/p2.1 a10/p2.2 a11/p2.3 a12/p2.4 p4.7/rst txd/p3.1 int/rxd/p3.0 int0/p3.2 int1/p3.3 clkout0/int/t0/p3.4 clkout1/int/t1/p3.5 mosi/adc5/p1.5 miso/adc6/p1.6 sclk/adc7/p1.7 sclk/txd2/ccp1/p4.3 vcc p1.4/adc4/ccp1/ss p1.3/adc3/ccp0/txd2 p1.2/adc2/eci/rxd2 p1.1/adc1 p1.0/adc0/clkout2 p0.0 p0.1 p0.2 p0.3 p4.2/ccp0rxd2/miso 40 1 ale/p4.5 na/p4.4 p0.4 p0.5 p0.6 ex_lvd/p4.6/rst2 p2.7/a15 p2.6/a14 p2.5/a13 p0.7 p4.7/rst txd/p3.1 int/rxd/p3.0 int0/p3.2 int1/p3.3 clkout0/int/t0/p3.4 clkout1/int/t1/p3.5 mosi/adc5/p1.5 sclk/adc7/p1.7 miso/adc6/p1.6 gnd qfn-40 stc12c5a60s2 xtal2 xtal1 p3.6/wr p3.7/rd p2.0/a8 p2.1/a9 p2.2/a10 p2.3/a11 p2.4/a12 vcc ss/ccp1/adc4/p1.4 txd2/ccp0/adc3/p1.3 rxd2/eci/adc2/p1.2 adc1/p1.1 clkout2/adc0/p1.0 p0.0 p0.1 p0.2 p0.3 register p4sw is used to set the secondary function of na/p4.4, ale/p4.5 and ex_lvd/p4.6 mnemonic add name 7 6 5 4 3 2 1 0 reset value p4sw bbh port-4 switch lvd_p4.6 ale_p4.5 na_p4.4 x000,xxxx na/p4.4: 0, p4sw.4=0 when mcu is reset. na/p4.4 is weak pull-up and no any function. 1, when p4sw.4 is set to 1, na/p4.4 is as an i/o port (p4.4) ale/p4.5: 0, p4sw.5=0 when mcu is reset. ale/p4.5 is as ale signal which is used to access external data memory . 1, when p4sw.5 is set to 1, ale/p4.4 is used as an i/o port (p4.5) lvd/p4.6: 0, p4sw.6=0 when mcu is reset. ex_lvd/p4.6 is as external low-voltage detection function 1, when p4sw.6 is set to 1, ex_lvd/p4.6 is used as an i/o port (p4.6) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 12 www.stcmcu.com register auxr1 is used to select whether pca/pwm/spi/uart2 function is on p1 port or p4 port mnemonic add name 7 6 5 4 3 2 1 0 reset value auxr1 a2h auxiliary register 1 - pca_p4 spi_p4 s2_p4 gf2 adrj - dps x000,00x0 pca _p4 0 : default. the pca function is on p1[4:2] 1 : the pca function on p1[4:2] is switched to p4[3:1]. eci is switched from p1.2 to p4.1 pca0/pwm0 is switched from p1.3 to p4.2 pca1/pwm1 is switched from p1.4 to p4.3 spi_p4 0 : default. the spi function is on p1[7:4] 1 : the spi function on p1[7:4] is switched to p4[3:0]. sclk is switched from p1.7 to p4.3 mosi is switched from p1.6 to p4.2 miso is switched from p1.5 to p4.1 ss is switched from p1.4 to p4.0 s2_p4 0 : default. the uart2(s2) function is on p1[3:2] 1 : the uart2(s2) function on p1[3:2] is switched to p4[3:2]. txd2 is switched from p1.3 to p4.3 rxd2 is switched from p1.2 to p4.2 gf2 : general flag. it can be used by software. adrj 0 : the 10-bit conversion result of adc is arranged as {adc_res[7:0], adc_resl[1:0]}. 1 : the 10-bit conversion result is right-justified, {adc_res[1:0], adc_resl[7:0]}. dps 0 : default. dptr0 is selected as data pointer. 1 : the secondary dptr is switched to use. in stc-isp writter/programmer, users can select what rst/p4.7 is used as. the pin rst/p4.7 is as reset function acquiescently, see the following figure. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 13 www.stcmcu.com 1.4 stc12c5a60s2 series selection table type 1t 8051 mcu operating voltage (v) flash (byte) s r a m (b) t i m e r u a r t d p t r e e p r o m (b) 16-bit pca/ 8-bit pwm d/a a/d w d t external real- time low voltage interrupt external reset threshold voltage can be configured external interrupts which can wake up power down mode package of 40-pin (36 i/o ports) package of 44-pin (40 i/o ports) package of 48-pin (44 i/o ports) stc12c5a60s2 series selection table stc12c5a08s2 5.5~3.5 8k 1280 4 2-3 2 53k 2 10 y y y 7 pdip40 lqfp/ plcc lqfp48 stc12c5a16s2 5.5~3.5 16k 1280 4 2-3 2 45k 2 10 y y y 7 pdip40 lqfp/ plcc lqfp48 stc12c5a32s2 5.5~3.5 32k 1280 4 2-3 2 29k 2 10 y y y 7 pdip40 lqfp/ plcc lqfp48 stc12c5a40s2 5.5~3.5 40k 1280 4 2-3 2 21k 2 10 y y y 7 pdip40 lqfp/ plcc lqfp48 stc12c5a48s2 5.5~3.5 48k 1280 4 2-3 2 13k 2 10 y y y 7 pdip40 lqfp/ plcc lqfp48 stc12c5a52s2 5.5~3.5 52k 1280 4 2-3 2 9k 2 10 y y y 7 pdip40 lqfp/ plcc lqfp48 stc12c5a56s2 5.5~3.5 56k 1280 4 2-3 2 5k 2 10 y y y 7 pdip40 lqfp/ plcc lqfp48 stc12c5a60s2 5.5~3.5 60k 1280 4 2-3 2 1k 2 10 y y y 7 pdip40 lqfp/ plcc lqfp48 iap12c5a62s2 5.5~3.5 62k 1280 4 2-3 2 iap 2 10 y y y 7 pdip40 lqfp/ plcc lqfp48 stc12le5a60s2 series selection table stc12le5a08s2 3.6~2.1 8k 1280 4 2-3 2 53k 2 10 y y y 7 pdip40 lqfp/ plcc lqfp48 stc12le5a16s2 3.6~2.1 16k 1280 4 2-3 2 45k 2 10 y y y 7 pdip40 lqfp/ plcc lqfp48 stc12le5a32s2 3.6~2.1 32k 1280 4 2-3 2 29k 2 10 y y y 7 pdip40 lqfp/ plcc lqfp48 stc12le5a40s2 3.6~2.1 40k 1280 4 2-3 2 21k 2 10 y y y 7 pdip40 lqfp/ plcc lqfp48 stc12le5a48s2 3.6~2.1 48k 1280 4 2-3 2 13k 2 10 y y y 7 pdip40 lqfp/ plcc lqfp48 stc12le5a52s2 3.6~2.1 52k 1280 4 2-3 2 9k 2 10 y y y 7 pdip40 lqfp/ plcc lqfp48 stc12le5a56s2 3.6~2.1 56k 1280 4 2-3 2 5k 2 10 y y y 7 pdip40 lqfp/ plcc lqfp48 stc12le5a60s2 3.6~2.1 60k 1280 4 2-3 2 1k 2 10 y y y 7 pdip40 lqfp/ plcc lqfp48 iap12le5a62s2 3.6~2.1 62k 1280 4 2-3 2 iap 2 10 y y y 7 pdip40 lqfp/ plcc lqfp48 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 14 www.stcmcu.com type 1t 8051 mcu operating voltage (v) flash (byte) s r a m (b) t i m e r u a r t d p t r 16-bit pca/ 8-bit pwm d/a a/d w d t e e p r o m (b) external real-time low voltage interrupt external reset threshold voltage can be configured external interrupts which can wake up power down mode package of 40-pin (36 i/o ports) package of 44-pin (40 i/o ports) package of 48-pin (44 i/o ports) stc12c5a60ad series selection table stc12c5a08ad 5.5~3.5 8k 1280 4 1 2 2 10 y 53k y y 7 pdip40 lqfp/ plcc lqfp48 stc12c5a16ad 5.5~3.5 16k 1280 4 1 2 2 10 y 45k y y 7 pdip40 lqfp/ plcc lqfp48 stc12c5a32ad 5.5~3.5 32k 1280 4 1 2 2 10 y 29k y y 7 pdip40 lqfp/ plcc lqfp48 stc12c5a40ad 5.5~3.5 40k 1280 4 1 2 2 10 y 21k y y 7 pdip40 lqfp/ plcc lqfp48 stc12c5a48ad 5.5~3.5 48k 1280 4 1 2 2 10 y 13k y y 7 pdip40 lqfp/ plcc lqfp48 stc12c5a52ad 5.5~3.5 52k 1280 4 1 2 2 10 y 9k y y 7 pdip40 lqfp/ plcc lqfp48 stc12c5a56ad 5.5~3.5 56k 1280 4 1 2 2 10 y 5k y y 7 pdip40 lqfp/ plcc lqfp48 stc12c5a60ad 5.5~3.5 60k 1280 4 1 2 2 10 y 1k y y 7 pdip40 lqfp/ plcc lqfp48 iap12c5a62ad 5.5~3.5 62k 1280 4 1 2 2 10 y iap y y 7 pdip40 lqfp/ plcc lqfp48 stc12le5a60ad series selection table stc12le5a08ad 3.6~2.1 8k 1280 4 1 2 2 10 y 53k y y 7 pdip40 lqfp/ plcc lqfp48 stc12le5a16ad 3.6~2.1 16k 1280 4 1 2 2 10 y 45k y y 7 pdip40 lqfp/ plcc lqfp48 stc12le5a32ad 3.6~2.1 32k 1280 4 1 2 2 10 y 29k y y 7 pdip40 lqfp/ plcc lqfp48 stc12le5a40ad 3.6~2.1 40k 1280 4 1 2 2 10 y 21k y y 7 pdip40 lqfp/ plcc lqfp48 stc12le5a48ad 3.6~2.1 48k 1280 4 1 2 2 10 y 13k y y 7 pdip40 lqfp/ plcc lqfp48 stc12le5a52ad 3.6~2.1 52k 1280 4 1 2 2 10 y 9k y y 7 pdip40 lqfp/ plcc lqfp48 stc12le5a56ad 3.6~2.1 56k 1280 4 1 2 2 10 y 5k y y 7 pdip40 lqfp/ plcc lqfp48 stc12le5a60ad 3.6~2.1 60k 1280 4 1 2 2 10 y 1k y y 7 pdip40 lqfp/ plcc lqfp48 iap12le5a62ad 3.6~2.1 62k 1280 4 1 2 2 10 y iap y y 7 pdip40 lqfp/ plcc lqfp48 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 15 www.stcmcu.com type 1t 8051 mcu operating voltage (v) flash (byte) s r a m (b) t i m e r u a r t d p t r 16-bit pca/ 8-bit pwm d/a a/ d w d t e e p r o m (b) external real- time low voltage interrupt external reset threshold voltage can be configured external interrupts which can wake up power down mode package of 40-pin (36 i/o ports) package of 44-pin (40 i/o ports) package of 48-pin (44 i/o ports) stc12c5a60pwm series selection table stc12c5a08pwm 5.5~3.5 8k 1280 4 1 2 2 n y 53k y y 7 pdip lqfp/ plcc lqfp48 stc12c5a16pwm 5.5~3.5 16k 1280 4 1 2 2 n y 45k y y 7 pdip lqfp/ plcc lqfp48 stc12c5a32pwm 5.5~3.5 32k 1280 4 1 2 2 n y 29k y y 7 pdip lqfp/ plcc lqfp48 stc12c5a40pwm 5.5~3.5 40k 1280 4 1 2 2 n y 21k y y 7 pdip lqfp/ plcc lqfp48 stc12c5a48pwm 5.5~3.5 48k 1280 4 1 2 2 n y 13k y y 7 pdip lqfp/ plcc lqfp48 stc12c5a52pwm 5.5~3.5 52k 1280 4 1 2 2 n y 9k y y 7 pdip lqfp/ plcc lqfp48 stc12c5a56pwm 5.5~3.5 56k 1280 4 1 2 2 n y 5k y y 7 pdip lqfp/ plcc lqfp48 stc12c5a60pwm 5.5~3.5 60k 1280 4 1 2 2 n y 1k y y 7 pdip lqfp/ plcc lqfp48 iap12c5a62pwm 5.5~3.5 62k 1280 4 1 2 2 n y iap y y 7 pdip lqfp/ plcc lqfp48 stc12le5a60pwm series selection table stc12le5a08pwm 3.6~2.1 8k 1280 4 1 2 2 n y 53k y y 7 pdip lqfp/ plcc lqfp48 stc12le5a16pwm 3.6~2.1 16k 1280 4 1 2 2 n y 45k y y 7 pdip lqfp/ plcc lqfp48 stc12le5a32pwm 3.6~2.1 32k 1280 4 1 2 2 n y 29k y y 7 pdip lqfp/ plcc lqfp48 stc12le5a40pwm 3.6~2.1 40k 1280 4 1 2 2 n y 21k y y 7 pdip lqfp/ plcc lqfp48 stc12le5a48pwm 3.6~2.1 48k 1280 4 1 2 2 n y 13k y y 7 pdip lqfp/ plcc lqfp48 stc12le5a52pwm 3.6~2.1 52k 1280 4 1 2 2 n y 9k y y 7 pdip lqfp/ plcc lqfp48 stc12le5a56pwm 3.6~2.1 56k 1280 4 1 2 2 n y 5k y y 7 pdip lqfp/ plcc lqfp48 stc12le5a60pwm 3.6~2.1 60k 1280 4 1 2 2 n y 1k y y 7 pdip lqfp/ plcc lqfp48 iap12le5a62pwm 3.6~2.1 62k 1280 4 1 2 2 n y iap y y 7 pdip lqfp/ plcc lqfp48 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 16 www.stcmcu.com 1.5 stc12c5a60s2 series minimum application system 31 30 29 28 27 26 25 24 23 22 21 40 39 38 37 36 35 34 33 32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 p0.3/ad3 clkout2/adc0/p1.0 adc1/p1.1 eci/adc2/p1.2 ccp0/adc3/p1.3 ss/ccp1/adc4/p1.4 mosi/adc5/p1.5 miso/adc6/p1.6 sclk/adc7/p1.7 rst/p4.7 rxd/p3.0 txd/p3.1 clkout0/t0/p3.4 clkout1/t1/p3.5 xtal2 xtal1 gnd vcc p0.0/ad0 p0.1/ad1 p0.2/ad2 p0.4/ad4 p0.5/ad5 p0.6/ad6 p0.7/ad7 ex_lvd/p4.6/rst2 ale/p4.5 na/p4.4 p2.7/ad15 p2.6/ad14 p2.5/ad13 p2.4/ad12 p2.3/ad11 p2.2/ad10 p2.1/ad9 p2.0/ad8 10f 104 c6 c5 c2<47pf c3<47pf x1 int0/p3.2 int1/p3.3 wr/p3.6 rd/p3.7 10f 10k c1 r1 + + system power /5v/3v vin sw1 power on about reset circuit: when the clock frequency is lower than 12mhz, it is suggested not to use c1 and r1 replaced by 1k resistor connect to ground when the clock frequencies is higher than 12mhz, it is recommended to use the second reset function pin (stc12c5a60s2 series on rst2/ex_lvd/p4.6 pin stc12c5201ad series on rst2/ex_lvd/p1.2 pin) about crystals circuit: if external clock frequency is higher than 33mhz, it is recommendedto directly use external active crystals. if using internal r/c oscillator clock (at the room temperature circumstance, the clock frequency of 5v mcu is 11mhz ~ 17mhz, 3v mcu's is 8mhz ~ 12mhz), xtal1 and xtal2 pin should be floated. if external clock frequency is in 27mhz above, we suggest to use the crystal that its nominal frequency is the fundamental frequency or directly use external active crystals which clock are input from xtal1 pin and xtal2 pin must be floated. but three partials crystals don't be used. otherwise as parameter improper collocation, it is possible to vibrate in the fundamental frequency, and then the actual frequency is only 1/3 of nominal frequency. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 17 www.stcmcu.com when the clock frequencies is higher than 12mhz, it is recommended to use the second reset function pin. c1 can be removed and r1 replaced by 1k resistor connect to ground. so the minimum application system is shown below 31 30 29 28 27 26 25 24 23 22 21 40 39 38 37 36 35 34 33 32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 p0.3/ad3 clkout2/adc0/p1.0 adc1/p1.1 eci/adc2/p1.2 ccp0/adc3/p1.3 ss/ccp1/adc4/p1.4 mosi/adc5/p1.5 miso/adc6/p1.6 sclk/adc7/p1.7 rst/p4.7 rxd/p3.0 txd/p3.1 clkout0/t0/p3.4 clkout1/t1/p3.5 xtal2 xtal1 gnd vcc p0.0/ad0 p0.1/ad1 p0.2/ad2 p0.4/ad4 p0.5/ad5 p0.6/ad6 p0.7/ad7 ex_lvd/p4.6/rst2 ale/p4.5 na/p4.4 p2.7/ad15 p2.6/ad14 p2.5/ad13 p2.4/ad12 p2.3/ad11 p2.2/ad10 p2.1/ad9 p2.0/ad8 9v - 12v vin sw1 power on 470f 104 c6 c5 c2<47pf c3<47pf x1 int0/p3.2 int1/p3.3 wr/p3.6 rd/p3.7 1k r1 7805 + r2 r3 10k 20k 5v + stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 18 www.stcmcu.com 1.6 stc12c5a60s2 series application circuit for isp 1 2 3 4 5 6 7 8 16 15 14 13 12 11 10 9 vcc gnd t1out r1in r1out t1in t2in r2out c1+ v+ c1- c2+ c2- v- t2out r2in u1-p1.0 u1-p1.1 mcu-vcc u1-p3.0 u1-p3.1 gnd 0.1f vcc vcc gnd pc_rxd(com pin2) pc_txd(com pin3) 2 3 5 1k 1k vcc 104 c6 1k mcu_rxd(p3.0) mcu_txd(p3.1) c2<47pf c1<47pf x1 usb+5v t1out r1in gnd usb1 this part of the circuit has nothing to do with the downloads stc3232,stc232,max232,sp232 pc com notes: traditional 8051's ale pin regardless of whether access to external data bus, will have a clock frequency output. the signals is a source of interference to the system. for this reason,stc mcu new added a enable/ disable ale signal output switch, thus reduced mcu internal to external electromagnetic emissions, improve system stability and reliability. if needs the signal as other peripheral device's clock source, you can get clock source from clkout0/p3.4, clkout1/p3.5, clkout2/p1.0 or xtal2 clock output. (recommended a 200ohm series resistor to the xtal2 pin) 31 30 29 28 27 26 25 24 23 22 21 40 39 38 37 36 35 34 33 32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ad3/p0.3 p1.0/adc0/clkout2 p1.1/adc1 p1.2/adc2/eci/rxd2 p1.3/adc3/ccp0/txd2 p1.4/adc4/ccp1/ss p1.5/adc5/mosi p1.6/adc6/miso p1.7/adc7/sclk rst/p4.7 p3.0/rxd/int p3.1/txd p3.4/t0/int/clkout0 p3.5/t1/int/clkout1 xtal2 xtal1 gnd vcc ad0/p0.0 ad1/p0.1 ad2/p0.2 ad4/p0.4 ad5/p0.5 ad6/p0.6 ad7/p0.7 rst2/lvd/p4.6 ale/p4.5 na/p4.4 ad15/p2.7 ad14/p2.6 ad13/p2.5 ad12/p2.4 ad11/p2.3 ad10/p2.2 ad9/p2.1 ad8/p2.0 p3.2/int0 p3.3/int1 p3.6/wr p3.7/rd + 10f 10k c1 r1 + vin sw1 power on 10f c5 + system power/usb +5v stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 19 www.stcmcu.com users in their target system, such as the p3.0/p3.1 through the rs-232 level shifter connected to the computer after the conversion of ordinary rs-232 serial port to connect the system programming / upgrading client software. if the user panel recommended no rs-232 level converter, should lead to a socket, with gnd/p3.1/ p3.0/vcc four signal lines, so that the user system can be programmed directly. of course, if the six signal lines can lead to gnd/p3.1/p3.0/vcc/p1.1/p1.0 as well, because you can download the program by p1.0/p1.1 isp ban. if you can gnd/p3.1/p3.0/vcc/p1.1/p1.0/reset seven signal lines leads to better, so you can easily use "offline download board (no computer)" . isp programming on the theory and application guide to see "stc12c5201ad series mcu development / programming tools help"section. in addition, we have standardized programming download tool, the user can then program into the goal in the above systems, you can borrow on top of it rs-232 level shifter connected to the computer to download the program used to do. programming a chip roughly be a few seconds, faster than the ordinary universal programmer much faster, there is no need to buy expensive third-party programmer?. pc stc-isp software downloaded from the website www.stcmcu.com stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 20 www.stcmcu.com 1.7 pin descriptions mnemonic lqfp44 lqfp48 pdip40 plcc44 qfn40 description p0.0 ~ p0.7 37-30 40 ~33 39-32 43~36 34~27 port0 : port0 is an 8-bit bi-directional i/o port with pull-up resistance. except being as gpio, port 0 is also the multiplexed low- order address and data bus during accesses to external program and data memory. p1.0/adc0/clkout2 40 43 1 2 36 port1 : general-purposed i/o with weak pull- up resistance inside. when 1s are written into port1, the strong output driving cmos only turn-on two period and then the weak pull-up resistance keep the port high. adcn : analog to digital converter input p1.1/adc1 41 44 2 3 37 p1.2/adc2/eci/rxd2 42 45 3 4 38 p1.3/adc3/ccp0/txd2 43 46 4 5 39 p1.4/adc4/ccp1/ss 44 47 5 6 40 p1.5/adc5/mosi 1 2 6 7 1 p1.6/adc6/miso 2 3 7 8 2 p1.7/adc7/sclk 3 4 8 9 3 p2.0 ~ p2.7 18-25 19-23 21-28 24~31 16~23 port2 : port2 is an 8-bit bi-directional i/o port with pull-up resistance. except being as gpio, port2 emits the high-order address byte during accessing to external program and data memory. 26-28 p3.0/rxd 5 6 10 11 5 port3 : general-purposed i/o with weak pull- up resistance inside. when 1s are written into port3, the strong output driving cmos only turn-on two period and then the weak pull- up resistance keep the port high. port3 also serves the functions of various special features of stc12c5a60s2. p3.1/txd 7 8 11 13 6 p3.2/int0 8 9 12 14 7 p3.3/int1 9 10 13 15 8 p3.4/t0/int/clkout0 10 11 14 16 9 p3.5/t1/int/clkout1 11 12 15 17 10 p3.6/wr 12 13 16 18 11 p3.7/rd 13 14 17 19 12 p4.0/ss 17 18 23 port4 : port4 are extended i/o ports such like port1. it can be available only on lqfp44, lqfp48, plcc44. ale : address latch enable. it is used for external data memory cycles (movx) ex_lvd : external low voltage reset detector p4.1/eci/mosi 28 31 34 p4.2/ccp0/miso 39 42 1 p4.3/ccp1/sclk 6 7 12 p4.4/na 26 29 29 32 24 p4.5/ale 27 30 30 33 25 p4.6/ex_lvd/rst2 29 32 31 35 26 p4.7/rst 4 5 9 10 4 rst 4 5 9 10 4 reset : a high on this pin for at least two machine cycles will reset the device. p5.0 24 port5 : port5 are extended i/o ports such like port1. it can be available only on lqfp48. p5.1 25 p5.2 48 p5.3 1 xtal1 15 16 19 21 14 crystal1 : input to the inverting oscillator amplifier. receives the external oscillator signal when an external oscillator is used. xtal2 14 15 18 20 13 crystal2 : output from the inverting amplifier. this pin should be floated when an external oscillator is used. vcc 38 41 40 44 35 power gnd 16 17 20 22 15 ground stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 21 www.stcmcu.com 1.8 package dimension drawings notes: 1.jedec outline:ms-026 bsb 2.dimensions d1 and e1 d0 not include mold protrusion. allowble protrusion is 0.25mm per side. d1 and e1 are maximum plastic body size dimensions imcluding mold mismatch. 3.dimension b does not include dambar protrusion.allowble dambar protrusion shall not cause the lead width to exceed the maximun b dimnsion by more than 0.08mm. lqfp-44 outline package d1 (10mm) d (12mm) e1 e 1 11 12 22 23 33 44 34 a a2 c1 b e 0.05max 0 0.25 l l1 gate plane seating plane a1 symbols min. nom max. a - - 1.60 a1 0.05 - 0.15 a2 1.35 1.40 1.45 c1 0.09 - 0.16 d 12.00 d1 10.00 e 12.00 e1 10.00 e 0.80 b(w/o plating) 0.25 0.30 0.35 l 0.45 0.60 0.75 l1 1.00ref 0 0 0 3.5 0 7 0 1 variations (all dimensions shown in mm 0.80mm stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 22 www.stcmcu.com lqfp-48 outline package d1 (7mm) d (9mm) e1 e e b a3 a2 a a1 r1 r2 l1 l l2 b b1 c c1 base metal with plating symbol min nom max a - - 1.60 a1 0.05 - 0.15 a2 1.35 1.40 1.45 a3 0.59 0.64 0.69 b 0.18 - 0.27 b1 0.17 0.20 0.23 c 0.13 - 0.18 c1 0.12 0.127 0.134 d 8.80 9.00 9.20 d1 6.90 7.00 7.10 e 8.80 9.00 9.20 e1 6.90 7.00 7.10 e 0.50 l 0.45 0.60 0.75 l1 1.00ref l2 0.25 r1 0.08 - - r2 0.08 - 0.20 s 0.20 - - 0.50mm variations (all dimensions shown in mm stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 23 www.stcmcu.com note: 1.jedec outline :ms-011 ac pdip-40 outline package 40 21 1 20 e1 d (2060mil) e c e 0 100mil b a1 a2 a seating plane l b1 h symbols dimensions in inch min nor max a - - 0.190 a1 0.015 - 0.020 a2 0.15 0.155 0.160 c 0.008 - 0.015 d 2.025 2.060 2.070 e 0.600 bsc e1 0.540 0.545 0.550 l 0.120 0.130 0.140 b1 0.015 - 0.021 b 0.045 - 0.067 e 0.630 0.650 0.690 0 0 7 15 unit: inch 1 inch = 1000mil stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 24 www.stcmcu.com note: 1.jedec outline :m0-047 ac 2.datum plane h is lacated at the bottom of the mold parting line coincident with where the lead exits the body. 3.dimensions e and d d0 not include mode protrusion. allowable protrusion is 10 mil pre side.dimensions e and d d0 include mold mismatch and are determined at datum plane h . 4.dimension b1 does not include dambar protrusion. plcc-44 outline package b a a2 a1 18 28 40 29 39 7 17 1 e he 6 d hd b1 gd l 0 ge seating plane y c h e symbols dimensions in inch dimensions in millmeters min nom max min nom max a 0.165 - 0.180 4.191 - 4.572 a1 0.020 - - 0.508 - - a2 0.147 - 0.158 3.734 - 4.013 b1 0.026 0.028 0.032 0.660 0.711 0.813 b 0.013 0.017 0.021 0.330 0.432 0.533 c 0.007 0.010 0.0013 0.178 0.254 0.330 d 0.650 0.653 0.656 16.510 16.586 16.662 e 0.650 0.653 0.656 16.510 16.586 16.662 0.050bsc 1.270bsc gd 0.590 0.610 0.630 14.986 15.494 16.002 ge 0.590 0.610 0.630 14.986 15.494 16.002 hd 0.685 0.690 0.695 17.399 17.526 17.653 he 0.685 0.690 0.695 17.399 17.526 17.653 l 0.100 - 0.112 2.540 - 2.845 y - - 0.004 - - 0.102 1 inch = 1000 mil e stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 25 www.stcmcu.com 4.50 4.800.05 5.100.05 0.05max #1 0.150.06 4.50 4.800.05 5.100.05 #40 0.40type 0.20 3.40 (r0.20) 40r0.10 3.40 1.21 0.400.06 4.800.05 5.100.05 2r0.152 max.0.05 max.0.75 0.203 ret 0.4870.03 qfn-40 outline package top view bottom view stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 26 www.stcmcu.com 1.9 stc12c5a60s2 series mcu naming rules stc12 xx 5a xx xx -- 37 x - xxxx xx pin number e.g. 40, 44,48 package type e.g. pdip,lqfp,plcc temperature range i : industrial, -40 -80 c : commercial, 0 -70 operating frequency 37 : up to 37mhz s2 : have secondary uart, adc function, pwm and internal eeprom ad : have adc function, pwm and internal eeprom, no secondary uart pwm: have pwm and internal eeprom,no secondary uart and adc function program space 08:8kb 16:16kb 20:20kb 20:20kb 32:32kb 40:40kb 48 60:60kb etc. operating voltage c : 5.5v~3.3v le : 2.2v~3.6v stc 1t series 8051 mcu speed is 8~12 times the traditional 8051 ram is up to1280 bytes stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 27 www.stcmcu.com 1.10 global unique identification number (id) stc 1t mcu 12c5axx series, each mcu has a unique identification number (id). user can use mov @ri instruction read ram unit f1~f7 to get the id number after power on. if users need to the unique identification number to encrypt their procedures, detecting the procedures not be illegally modified should be done first. //the following example program written by c language is to read internal id number /*----------------------------------------------------------------------------------*/ /* --- stc mcu international limited -------------------------------------*/ /* --- mobile: 13922809991 ------------------------------------------------ */ /* --- fax: 0755-82905966 ------------------------------------------------- */ /* --- tel: 0755-82948409 -------------------------------------------------- */ /* --- web: www.stcmcu.com --------------------------------------------*/ /* if you want to use the program or the program referenced in the --*/ /* article, please specify in which data and procedures from stc --*/ /*---------------------------------------------------------------------------------*/ #include #include sfr iap_contr = 0xc7; sbit mcu_start_led = p1^7; //unsigned char self_command_array[4] = {0x22,0x33,0x44,0x55}; #define self_define_isp_download_command 0x22 #define reload_count 0xfb //18.432mhz,12t,smod=0,9600bps void serial_port_initial(); void send_uart(unsigned char); void uart_interrupt_receive(void); void soft_reset_to_isp_monitor(void); void delay(void); void display_mcu_start_led(void); void main(void) { unsigned char i = 0; unsigned char j = 0; unsigned char idata *idata_point; stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 28 www.stcmcu.com serial_port_initial(); //initialize serial port // display_mcu_start_led(); //mcu begin to run when led is be lighted // send_uart(0x34); // send_uart(0xa7); idata_point = 0xf1; for(j=0;j<=6; j++) { i = *idata_point; send_uart(i); idata_point++; } while(1); } void serial_port_initial() { scon = 0x50; //0101,0000 8-bit variable baud rate no parity tmod = 0x21; //0011,0001 timer1 as 8-bit auto-reload timer th1 = reload_count; //set the auto-reload parameter tl1 = reload_count; tr1 = 1; es = 1; ea = 1; } void send_uart(unsigned char i) { es = 0; ti = 0; sbuf = i; while(ti ==0); ti = 0; es = 1; } void uart_interrupt_receive(void) interrupt 4 { unsigned char k = 0; if(ri==1) { ri = 0; k = sbuf; stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 29 www.stcmcu.com if(k==self_define_isp_download_command) //self-define download command { delay(); //just delay 1 second delay(); soft_reset_to_isp_monitor(); //soft rese to isp monitor } send_uart(k); } else { ti = 0; } } void soft_reset_to_isp_monitor(void) { iap_contr = 0x60; //0110,0000 soft rese to isp monitor } void delay(void) { unsigned int j = 0; unsigned int g = 0; for(j=0;j<5;j++) { for(g=0;g<60000;g++) { _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); } } } void display_mcu_start_led(void) { unsigned char i = 0; for(i=0;i<3;i++) { mcu_start_led = 0; delay(); mcu_start_led = 1; delay(); mcu_start_led = 0; } } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 30 www.stcmcu.com chapter 2. clock, power management and reset 2.1 clock stc12c5a60s2 series is stc 1t mcu whose system clock is compatible with traditional 8051 mcu. there are two clock sources available for stc12c5a60s2. one is the clock from crystal oscillation and the other is from internal simple rc oscillator. the internal built-in rc oscillator can be used to replace the external crystal oscillator in the application which doesn't need an exact system clock. to enable the built-in oscillator, user should enable the option on-chip r/c clock by stc-isp writer/programmer. external crystal/clock is selected first in stc-isp writer/programmer because the manufacturer's selection of stc12c5a60s2 series is external crystal/clock. 2.1.1 on-chip r/c clock and external crystal/clcok are optional in stc-isp.exe after next-power up/ cold reset mcu clock can be: 1. on-chip r/c clock 2. external crystal/clock stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 31 www.stcmcu.com 2.1.2 divider for system clock a clock divider(clk_div) is designed to slow down the operation speed of stc12c5a60s2, to save the operating power dynamically. user can slow down the mcu by means of writing a non-zero value to the clks[2:0] bits in the clk_div register. this feature is especially useful to save power consumption in idle mode as long as the user changes the clks[2:0] to a non-zero value before entering the idle mode. clk_div register (clock divider) sfr name sfr address bit b7 b6 b5 b4 b3 b2 b1 b0 clk_div 97h name - - - - - clks2 clks1 clks0 b2-b0 (clks2-clks0) : 000 external crystal/clock or on-chip r/c clock is not divided (default state) 001 external crystal/clock or on-chip r/c clock is divided by 2. 010 external crystal/clock or on-chip r/c clock is divided by 4. 011 external crystal/clock or on-chip r/c clock is divided by 8. 100 external crystal/clock or on-chip r/c clock is divided by 16. 101 external crystal/clock or on-chip r/c clock is divided by 32. 110 external crystal/clock or on-chip r/c clock is divided by 64. 111 external crystal/clock or on-chip r/c clock is divided by 128. not-divided 2 4 8 16 32 64 128 clks2,clks1,clks0 000 001 010 011 100 101 110 111 system clock (to cpu and peripherals) clock structure on-chip r/c clock external crystal/clock stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 32 www.stcmcu.com 2.1.3 how to know internal rc oscillator frequency(internal clock frequency) stc 1t mcu 12c5axx series in addition to traditional external clock, but also the option of using the internal rc oscillator clock source. if select internal rc oscillator, external crystal can be saved. xtal1 and xtal2 floating. relatively large errors due to internal clock, so high requirements on the timing or circumstances have serial communication is not recommended to use the internal oscillator. user can use mov @ri instruction read ram unit fc~ff to get the internal oscillator frequency of the factory and read ram unit f8~fb to get internal oscillator frequency of last used to download programs within the internal oscillator after power on. //the following example program written by c language is to read internal r/c clock frequency /*----------------------------------------------------------------------------------*/ /* --- stc mcu international limited -------------------------------------*/ /* --- mobile: 13922809991 ------------------------------------------------ */ /* --- fax: 0755-82905966 ------------------------------------------------- */ /* --- tel: 0755-82948409 -------------------------------------------------- */ /* --- web: www.stcmcu.com --------------------------------------------*/ /* if you want to use the program or the program referenced in the --*/ /* article, please specify in which data and procedures from stc --*/ /*---------------------------------------------------------------------------------*/ #include #include sfr iap_contr = 0xc7; sbit mcu_start_led = p1^7; //unsigned char self_command_array[4] = {0x22,0x33,0x44,0x55}; #define self_define_isp_download_command 0x22 #define reload_count 0xfb //18.432mhz,12t,smod=0,9600bps void serial_port_initial(); void send_uart(unsigned char); void uart_interrupt_receive(void); void soft_reset_to_isp_monitor(void); void delay(void); void display_mcu_start_led(void); void main(void) { unsigned char i = 0; unsigned char j = 0; unsigned char idata *idata_point; stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 33 www.stcmcu.com serial_port_initial(); //initialize serial port // display_mcu_start_led(); //mcu begin to run when led is be lighted // send_uart(0x34); // send_uart(0xa7); idata_point = 0xfc; for(j=0;j<=3;j++) { i = *idata_point; send_uart(i); idata_point++; } while(1); } void serial_port_initial() { scon = 0x50; //0101,0000 8-bit variable baud rate no parity tmod = 0x21; //0011,0001 timer1 as 8-bit auto-reload timer th1 = reload_count; //set the auto-reload parameter tl1 = reload_count; tr1 = 1; es = 1; ea = 1; } void send_uart(unsigned char i) { es = 0; ti = 0; sbuf = i; while(ti ==0); ti = 0; es = 1; } void uart_interrupt_receive(void) interrupt 4 { unsigned char k = 0; if(ri==1) { ri = 0; k = sbuf; stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 34 www.stcmcu.com if(k==self_define_isp_download_command) //self-define download command { delay(); //just delay 1 second delay(); soft_reset_to_isp_monitor(); //soft rese to isp monitor } send_uart(k); } else { ti = 0; } } void soft_reset_to_isp_monitor(void) { iap_contr = 0x60; //0110,0000 soft rese to isp monitor } void delay(void) { unsigned int j = 0; unsigned int g = 0; for(j=0;j<5;j++) { for(g=0;g<60000;g++) { _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); } } } void display_mcu_start_led(void) { unsigned char i = 0; for(i=0;i<3;i++) { mcu_start_led = 0; delay(); mcu_start_led = 1; delay(); mcu_start_led = 0; } } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 35 www.stcmcu.com 2.1.4 programmable clock output stc12c5a60s2 series mcu have three channel programmable clock outputs, they are timer 0 programmable clock output clkout0(p3.4/t0), timer 1 programmable clock output clkout1(p3.5/t1) and dedicated baud-rate timer programmable clock output (clkout2/p1.0). there are some sfrs about programmable clock output as shown below. the satement (used in c language) of special function registers auxr/wake_clko/brt: sfr auxr = 0x8e; //the address statement of special function register auxr sfr wake_clko = 0x8f; //the address statement of sfr wake_clko sfr brt = 0x9c; //the address statement of special function register brt the satement (used in assembly language) of special function registers auxr/wake_clko/brt: auxr equ 0x8e ;the address statement of special function register auxr wake_clko equ 0x8f ;the address statement of sfr wake_clko brt equ 0x9c ;the address statement of special function register brt symbol description address bit address and symbol msb lsb value after power-on or reset auxr auxiliary register 8eh t0x12 t1x12 uart_m0x6 brtr s2smod brtx12 extram s1brs 0000 0000b wake_clko clk_output power down wake-up control register 8fh pcawakeup rxd_pin_ie t1_pin_ie t0_pin_ie lvd_wake brtclko t1clko t0clko 0000 0000b brt dedicated baud- rate timer register 9ch 0000 0000b 1. auxr: auxiliary register (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 auxr 8eh name t0x12 t1x12 uart_m0x6 brtr s2smod brtx12 extram s1brs t0x12 : timer 0 clock source bit. 0 : the clock source of timer 0 is sysclk/12. it will compatible to the traditional 80c51 mcu 1 : the clock source of timer 0 is sysclk/1. it will drive the t0 faster than a traditional 80c51 mcu t1x12 : timer 1 clock source bit. 0 : the clock source of timer 1 is sysclk/12. it will compatible to the traditional 80c51 mcu 1 : the clock source of timer 1 is sysclk/1. it will drive the t0 faster than a traditional 80c51 mcu uart_m0x6 : baud rate select bit of uart1 while it is working under mode-0 0 : the baud-rate of uart in mode 0 is sysclk/12. 1 : the baud-rate of uart in mode 0 is sysclk/2. brtr : dedicated baud-rate timer run control bit. 0 : the baud-rate generator is stopped. 1 : the baud-rate generator is enabled. s2smod : the baud-rate of uart2 double contol bit. 0 : default. the baud-rate of uart2 (s2) is not doubled. 1 : the baud-rate uart2 (s2) is doubled. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 36 www.stcmcu.com brtx12 : dedicated baud-rate timer counter control bit. 0 : the baud-rate generator is incremented every 12 system clocks. 1 : the baud-rate generator is incremented every system clock. extram : internal / external ram access control bit. 0 : on-chip auxiliary ram is enabled and located at the address 0x0000 to 0x03ff. for address over 0x03ff, off-chip expanded ram becomes the target automatically. 1 : on-chip auxiliary ram is always disabled. s1brs : the baud-rate generator of uart1 select bit. 0 : default. select timer 1 as the baud-rate generator of uart1 1 : timer 1 is replaced by the independent baud-rate generator which is selected as the baud-rate of uart. in other words, timer 1 is released to use in other functions. 2. wake_clko: clk_output power down wake-up control register (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 wake_clko 8fh name pcawakeup rxd_pin_ie t1_pin_ie t0_pin_ie lvd_wake brtclko t1clko t0clko pcawakeup: when set and the associated-pca interrupt control registers is configured correctly, the cexn pin of pca function is enabled to wake up mcu from power-down state. rxd_pin_ie: when set and the associated-uart interrupt control registers is configured correctly, the rxd pin (p3.0) is enabled to wake up mcu from power-down state. t1_pin_ie : when set and the associated-timer1 interrupt control registers is configured correctly, the t1 pin (p3.5) is enabled to wake up mcu from power-down state. t0_pin_ie : when set and the associated-timer0 interrupt control registers is configured correctly, the t1 pin (p3.4) is enabled to wake up mcu from power-down state. lvd_wake: when set and the associated-lvd interrupt control registers is configured correctly, the cmpin pin is enabled to wake up mcu from power-down state. brtcklo : when set, p1.0 is enabled to be the clock output of baud-rate timer (brt). the clock rate is brg overflow rate divided by 2. t1cklo : when set, p3.5 is enabled to be the clock output of timer 1. the clock rate is timer 1overflow rate divided by 2. t0cklo : when set, p3.4 is enabled to be the clock output of timer 0. the clock rate is timer 0overflow rate divided by 2. 3. brt: dedicated baud-rate timer register (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 brt 9ch name it is used as the reload register for generating the baud-rate of the uart. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 37 www.stcmcu.com 2.1.4.1 timer 0 programmable clock-out on p3.4 timer/counter 0 mode 2: 8-bit auto-reload sysclk control c/t=0 c/t=1 t0 pin tr0 gate int0 auxr.7/t0x12=0 auxr.7/t0x12=1 tl0 (8 bits) th0 (8 bits) 12 1 interrupt tf0 toggle t0clko p3.4 clkout0 stc12c5201ad is able to generate a programmable clock output on p3.4. when t0clko/ wake_clko.0 bit in wake_clko sfr is set, t0 timer overflow pulse will toggle p3.4 latch to generate a 50% duty clock. the frequency of clock-out = t0 overflow rate /2. if c/t (tmod.2) = 0, timer/counter 0 is set for timer operation (input from internal system clock), the frequency of clock-out is as following : (sysclk) / (256 C th0) / 2, when auxr.7 / t0x12=1 or (sysclk / 12) / (256 C th0) / 2 , when auxr.7 / t0x12=0 if c/t (tmod.2) = 1, timer/counter 0 is set for conter operation (input from external p3.4/t0 pin), the frequency of clock-out is as following : t0_pin_clk / (256-th0) / 2 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 38 www.stcmcu.com 2.1.4.2 timer 1 programmable clock-out on p3.5 stc12c5201ad is able to generate a programmable clock output on p3.5. when t1clko/wake_clko.1 bit in wake_clko sfr is set, t1 timer overflow pulse will toggle p3.5 latch to generate a 50% duty clock. the frequency of clock-out = t1 overflow rate /2. if c/t (tmod.6) = 0, timer/counter 1 is set for timer operation (input from internal system clock), the frequency of clock-out is as following : (sysclk) / (256 C th1) / 2, when auxr.6 / t0x12=1 or (sysclk / 12) / (256 C th1) / 2 , when auxr.6 / t0x12=0 if c/t (tmod.6) = 1, timer/counter 1 is set for conter operation (input from external p3.5/t1 pin), the frequency of clock-out is as following : t1_pin_clk / (256-th1) / 2 timer/counter 1 mode 2: 8-bit auto-reload interrupt sysclk tf1 control c/t=0 c/t=1 t1 pin tr1 gate int1 auxr.6/t1x12=0 auxr.6/t1x12=1 tl1 (8 bits) th1 (8 bits) 12 1 toggle t1clko p3.5 clkout1 2.1.4.3 baud rate generator and programmable clock output on p1.0 8 bits timer brt toggle brtclko p1.0 stc12c5201ad is able to generate a programmable clock output on p1.0. when brtclko bit in wake_clko is set, brt timer overflow pulse will toggle p1.0 latch to generate a 50% duty clock. the frequency of clock-out = baud-rate timer overflow rate /2. namely the frequency of clock-out is shown as below : (sysclk) / (256 Cbrt) /2, when brtx12=1 or (sysclk/12) / (256 C brt) /2 , when brtx12=0 sysclk auxr.2/brtx12=0 auxr.2/brtx12=1 12 1 auxr.4 / brtr clkout2 to uart overflow stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 39 www.stcmcu.com 2.2 power management modes pcon register (power control register) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 pcon 87h name smod smod0 lvdf pof gf1 gf0 pd idl smod : double baud rate of uart interface 0 keep normal baud rate when the uart is used in mode 1,2 or 3. 1 double baud rate bit when the uart is used in mode 1,2 or 3. smod0 : sm0/fe bit select for scon.7; setting this bit will set scon.7 as frame error function. clearing it to set scon.7 as one bit of uart mode selection bits. lvdf : pin low-voltage flag. once low voltage condition is detected (vcc power is lower than lvd voltage), it is set by hardware (and should be cleared by software). pof : power-on flag. it is set by power-off-on action and can only cleared by software. practical application: if it is wanted to know which reset the mcu is used, see the following figure. in initializtion program, judge whether pof/pcon.4 have been set or not pof=1, yes cold boot power-on reset clear pof/pcon.4 pof=0, no external manual reset or wdt reset or software reset or others the stc12c5a60s2 core has three software programmable power management mode: slow-down, idle and stop/power-down mode. the power consumption of stc12c5a60s2 series is about 2ma~7ma in normal operation, while it is lower than 0.1ua in stop/power-down mode and 1.3ma in idle mode. slow-down mode is controlled by clock divider register(clk_div). idle and stop/power-down is managed by the corresponding bit in power control (pcon) register which is shown in below. gf1,gf0: general-purposed flag 1 and 0 pd : stop mode/ power-down select bit. . setting this bit will place the stc12c5a60s2 mcu in stop/power-down mode. stop/power-down mode can be waked up by external interrupt. because the mcu s internal oscillator stopped in stop/ power-down mode, cpu, timers, uarts and so on stop to run, only external interrupt go on to work. the following pins can wake up mcu from stop/power-down mode: int0 /p3.2, int1 /p3.3, int /t0/p3.4, int /t1/p3.5, int /rxd/p3.0, ccp0/p1.3(o r p4.2), ccp1/p1.4(or p4.3), ex_lvd/p4.6 p4.2), ccp1/p1.4(or p4.3), ex_lvd/p4.6 p4.2), ccp1/p1.4( or p4.3), ex_lvd/p4.6 or p4.3), ex_lvd/p4.6 p4.3), ex_lvd/p4.6 idl : idle mode select bit. setting this bit will place the stc12c5a60s2 in idle mode. only cpu goes into idle mode. (shuts off clock to cpu, but clock to timers, interrupts, serial ports, and analog peripherals are still active.) the following pins can wake up mcu from idle mode: int0 /p3.2, int1 /p3.3, int /t0/p3.4, int /t1/p3.5, int /rxd/p3.0. besides, timer0 and timer1 and uarts interrupt also can wake up mcu from idle mode stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 40 www.stcmcu.com 2.2.1 slow down mode a divider is designed to slow down the clock source prior to route to all logic circuit. the operating frequency of internal logic circuit can therefore be slowed down dynamically , and then save the power. user can slow down the mcu by means of writing a non-zero value to the clks[2:0] bits in the clk_div register. this feature is especially useful to save power consumption in idle mode as long as the user changes the clks[2:0] to a non-zero value before entering the idle mode. clk_div register (clock divider) sfr name sfr address bit b7 b6 b5 b4 b3 b2 b1 b0 clk_div 97h name - - - - - clks2 clks1 clks0 b2-b0 (clks2-clks0) : 000 external crystal/clock or on-chip r/c clock is not divided (default state) 001 external crystal/clock or on-chip r/c clock is divided by 2. 010 external crystal/clock or on-chip r/c clock is divided by 4. 011 external crystal/clock or on-chip r/c clock is divided by 8. 100 external crystal/clock or on-chip r/c clock is divided by 16. 101 external crystal/clock or on-chip r/c clock is divided by 32. 110 external crystal/clock or on-chip r/c clock is divided by 64. 111 external crystal/clock or on-chip r/c clock is divided by 128. not-divided 2 4 8 16 32 64 128 clks2,clks1,clks0 000 001 010 011 100 101 110 111 system clock (to cpu and peripherals) clock structure on-chip r/c clock external crystal/clock stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 41 www.stcmcu.com 2.2.2 idle mode an instruction that sets idl/pcon.0 causes that to be the last instruction executed before going into the idle mode, the internal clock is gated off to the cpu but not to the interrupt, timer, pca, spi, adc, wdt and serial port functions. the pca can be programmed either to pause or continue operating during idle. the cpu status is preserved in its entirety: the ram, stack pointer, program counter, program status word, accumulator, and all other registers maintain their data during idle. the port pins hold the logical states they had at the time idle was activated. ale and psen hold at logic high levels . idle mode leaves the peripherals running in order to allow them to wake up the cpu when an interrupt is generated. timer 0, timer 1, pwm timer and uart will continue to function during idle mode. there are two ways to terminate the idle. activation of any enabled interrupt will cause idl/pcon.0 to be cleared by hardware, terminating the idle mode. the interrupt will be serviced, and following reti, the next instruction to be executed will be the one following the instruction that put the device into idle. the flag bits (gfo and gf1) can be used to give art indication if an interrupt occurred during normal operation or during idle. for example, an instruction that activates idle can also set one or both flag bits. when idle is terminated by an interrupt, the interrupt service routine can examine the flag bits. the other way to wake-up from idle is to pull reset high to generate internal hardware reset. since the clock oscillator is still running, the hardware reset neeeds to be held active for only two machine cycles(24 oscillator periods) to complete the reset. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 42 www.stcmcu.com 2.2.3 stop / power down (pd) mode an instruction that sets pd/pcon.1 cause that to be the last instruction executed before going into the power- down mode. in the power-down mode, the on-chip oscillator and the flash memory are stopped in order to minimize power consumption. only the power-on circuitry will continue to draw power during power-down. the contents of on-chip ram and sfrs are maintained. the power-down mode can be woken-up by reset pin, external interrupt int0 ~ int1, rxd pin, t0 pin, t1 pin and pca input pinsccp0 pin and ccp1 pin. when it is woken-up by reset, the program will execute from the address 0x0000. be carefully to keep reset pin active for at least 10ms in order for a stable clock. if it is woken-up from i/o, the cpu will rework through jumping to related interrupt service routine. before the cpu rework, the clock is blocked and counted until 32768 in order for denouncing the unstable clock. to use i/o wake-up, interrupt-related registers have to be enabled and programmed accurately before power-down is entered. pay attention to have at least one nop instruction subsequent to the power-down instruction if i/o wake-up is used. when terminating power-down by an interrupt, the wake up period is internally timed. at the negative edge on the interrupt pin, power-down is exited, the oscillator is restarted, and an internal timer begins counting. the internal clock will be allowed to propagate and the cpu will not resume execution until after the timer has reached internal counter full. after the timeout period, the interrupt service routine will begin. to prevent the interrupt from re-triggering, the interrupt service routine should disable the interrupt before returning. the interrupt pin should be held low until the device has timed out and begun executing. the user should not attempt to enter (or re-enter) the power-down mode for a minimum of 4 us until after one of the following conditions has occured: start of code execution(after any type of reset), or exit from power-down mode. i/o intx 0.1uf 5m the following circuit can timing wake up mcu from power down mode when external interrupt sources do not exist operation step: 1. i/o ports are first configured to push-pull output(strong pull-up) mode 2. writen 1s into ports i/o ports 3. the above circuit will charge the capacitor c1 4. writen 0s into ports i/o ports, mcu will go into power-down mode 5. the above circuit will discharge. when the electricity of capacitor c1 has been discharged less than 0.8v, external interrupt intx pin will generate a falling edge and wake up mcu from power-down mode automatically. 300 r1 c1 i i stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 43 www.stcmcu.com the following example c program demostrates that power-down mode be woken-up by external interrupt . /*--------------------------------------------------------------------------------*/ /* --- stc mcu international limited -----------------------------------*/ /* --- stc 1t series mcu wake up power-down mode demo ------*/ /* --- mobile: (86)13922809991 ------------------------------------------*/ /* --- fax: 86-755-82905966 ----------------------------------------------*/ /* --- tel: 86-755-82948412 -----------------------------------------------*/ /* --- web: www.stcmcu.com -----------------------------------------*/ /* if you want to use the program or the program referenced in the */ /* article, please specify in which data and procedures from stc */ /*-------------------------------------------------------------------------------*/ #include #include sbit begin_led = p1^2; //begin-led indicator indicates system start-up unsigned char is_power_down = 0; //set this bit before go into power-down mode sbit is_power_down_led_int0 = p1^7; //power-down wake-up led indicator on int0 sbit not_power_down_led_int0 = p1^6; //not power-down wake-up led indicator on int0 sbit is_power_down_led_int1 = p1^5; //power-down wake-up led indicator on int1 sbit not_power_down_led_int1 = p1^4; //not power-down wake-up led indicator on int1 sbit power_down_wakeup_pin_int0 = p3^2; //power-down wake-up pin on int0 sbit power_down_wakeup_pin_int1 = p3^3; //power-down wake-up pin on int1 sbit normal_work_flashing_led = p1^3; //normal work led indicator void normal_work_flashing (void); void int_system_init (void); void int0_routine (void); void int1_routine (void); void main (void) { unsigned char j = 0; unsigned char wakeup_counter = 0; //clear interrupt wakeup counter variable wakeup_counter begin_led = 0; //system start-up led int_system_init ( ); //interrupt system initialization while(1) { p2 = wakeup_counter; wakeup_counter++; for(j=0; j<2; j++) { normal_work_flashing( ); //system normal work } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 44 www.stcmcu.com is_power_down = 1; //set this bit before go into power-down mode pcon = 0x02; //after this instruction, mcu will be in power-down mode //external clock stop _nop_( ); _nop_( ); _nop_( ); _nop_( ); } } void int_system_init (void) { it0 = 0; /* external interrupt 0, low electrical level triggered */ // it0 = 1; /* external interrupt 0, negative edge triggered */ ex0 = 1; /* enable external interrupt 0 it1 = 0; /* external interrupt 1, low electrical level triggered */ // it1 = 1; /* external interrupt 1, negative edge triggered */ ex1 = 1; /* enable external interrupt 1 ea = 1; /* set global enable bit } void int0_routine (void) interrupt 0 { if (is_power_down) { //is_power_down ==1; /* power-down wakeup on int0 */ is_power_down = 0; is_power_down_led_int0 = 0; /*open external interrupt 0 power-down wake-up led indicator */ while (power_down_wakeup_pin_int0 == 0) { /* wait higher */ } is_power_down_led_int0 = 1; /* close external interrupt 0 power-down wake-up led indicator */ } else { not_power_down_led_int0 = 0; /* open external interrupt 0 normal work led */ while (power_down_wakeup_pin_int0 ==0) { /* wait higher */ } not_power_down_led_int0 = 1; /* close external interrupt 0 normal work led */ } } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 45 www.stcmcu.com void int1_routine (void) interrupt 2 { if (is_power_down) { //is_power_down ==1; /* power-down wakeup on int1 */ is_power_down = 0; is_power_down_led_int1= 0; /*open external interrupt 1 power-down wake-up led indicator */ while (power_down_wakeup_pin_int1 == 0) { /* wait higher */ } is_power_down_led_int1 = 1; /* close external interrupt 1 power-down wake-up led indicator */ } else { not_power_down_led_int1 = 0; /* open external interrupt 1 normal work led */ while (power_down_wakeup_pin_int1 ==0) { /* wait higher */ } not_power_down_led_int1 = 1; /* close external interrupt 1 normal work led */ } } void delay (void) { unsigned int j = 0x00; unsigned int k = 0x00; for (k=0; k<2; ++k) { for (j=0; j<=30000; ++j) { _nop_( ); _nop_( ); _nop_( ); _nop_( ); _nop_( ); _nop_( ); _nop_( ); _nop_( ); } } } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 46 www.stcmcu.com void normal_work_flashing (void) { normal_work_flashing_led = 0; delay ( ); normal_work_flashing_led = 1; delay ( ); } the following program also demostrates that power-down mode or idle mode be woken-up by external interrupt, but is written in assembly language rather than c languge. /*--------------------------------------------------------------------------------*/ /* --- stc mcu international limited -----------------------------------*/ /* --- stc 1t series mcu wake up power-down mode demo ------*/ /* --- mobile: (86)13922809991 ------------------------------------------*/ /* --- fax: 86-755-82905966 ----------------------------------------------*/ /* --- tel: 86-755-82948412 -----------------------------------------------*/ /* --- web: www.stcmcu.com -----------------------------------------*/ /* if you want to use the program or the program referenced in the */ /* article, please specify in which data and procedures from stc */ /*-------------------------------------------------------------------------------*/ ;************************************************************** ;wake up idle and wake up power down ;************************************************************** org 0000h ajmp main org 0003h int0_interrupt: clr p1.7 ;open p1.7 led indicator acall delay ;delay in order to observe clr ea ;clear global enable bit, stop all interrupts reti org 0013h int1_interrupt: clr p1.6 ;open p1.6 led indicator acall delay ;;delay in order to observe clr ea ;clear global enable bit, stop all interrupts reti org 0100h delay: clr a mov r0, a mov r1, a mov r2, #02 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 47 www.stcmcu.com delay_loop: djnz r0, delay_loop djnz r1, delay_loop djnz r2, delay_loop ret main: mov r3, #0 ;p1 led increment mode changed ;start to run program main_loop: mov a, r3 cpl a mov p1, a acall delay inc r3 mov a, r3 subb a, #18h jc main_loop mov p1, #0ffh ;close all led, mcu go into power-down mode clr it0 ;low electrical level trigger external interrupt 0 ; setb it0 ;negative edge trigger external interrupt 0 setb ex0 ;enable external interrupt 0 clr it1 ;low electrical level trigger external interrupt 1 ; setb it1 ;negative edge trigger external interrupt 1 setb ex1 ;enable external interrupt 1 setb ea ;set the global enable ;if don't so, power-down mode cannot be wake up ;mcu will go into idle mode or power-down mode after the following instructions mov pcon, #00000010b ;set pd bit, power-down mode (pd = pcon.1) ; nop ; nop ; nop ; mov pcon, #00000001b ;set idl bit, idle mode (idl = pcon.0) mov p1, #0dfh ;1101,1111 nop nop nop wait1: sjmp wait1 ;dynamically stop end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 48 www.stcmcu.com 2.3 reset sources in stc12c5a60s2, there are 5 sources to generate internal reset. they are rst pin reset, external low-voltage detection (p4.6/rst2, the secondary reset function pin rst2 reset) , software reset , on-chip power-on-reset(if delay 200ms after power-on reset, the reset mode is on-chip max810 por timing delay which actully add 200ms delay after power-on reset) and watch-dog-timer reset. 2.3.1 reset pin external rst pin reset accomplishes the mcu reset by forcing a reset pulse to rst pin from external. the p4.7/rst pin is as reset function pin (default). if users need to configure it as i/o port (must use external clock), they may enable the corresponding option in stc-isp writter/programmer shown the following figure. if p4.7/rst pin is not be configured as i/o port, it will be as reset function pin (default) which is the input to schmitt trigger and input pin for chip reset. asser t ing an active-high signal and keeping at least 24 cycles plus 10us on the rst pin generates a reset. if the signal on rst pin changed active-low level, mcu will end the reset state and start to run from the 0000h of user procedures. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 49 www.stcmcu.com user can configure p4.6 as secondary reset pin through setting this option stc12c5a60s2 series mcu add secondary reset function(rst2/p4.6). the secondary reset function pin rst2 should be configured in stc-isp writter/programmer shown in the following figure. external reset threshold voltage is adjustable by means of 2 resistors. when system frequency is up to 12mhz, the secondary reser fuction is recommended to use. 2.3.2 external low voltage detection reset (high reliable reset, rst2 pin reset) typical application circuit, using ex_lvd pin achieve low-voltage-reset function, as shown below. if power inputing source is 5v dc, then the reference application circuit as bellow: (note : 7805 output 4v voltage and use r1 and r2 can achieve the low voltage reset function at 1.33v) 470f + vcc mcu p4.6/rst2 gnd 7805 + 104 r1 r2 ? >100k if power inputing source is 220v ac, then the reference application circuit as bellow: (note : 7805 output 8.5v voltage and use r1 and r2 can achieve the low voltage reset function at 1.33v ) 470f + vcc mcu p4.6/rst2 gnd 7805 + 104 r1 r2 10k 20k 104 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 50 www.stcmcu.com 2.3.3 external low voltage detection if not be used rst2 can be as interrupt the external low voltage detection function added in p4.6 port of stc12c5a60s2 series mcu can generate an interrupt when the vcc power drops down to the lvd voltage. user can detect whether the external voltage is over low by using interrupt or polling. the low voltage detection (lvd) voltage is 1.33v(5%) and 1.31v (3%) for stc12c5a60s2 and stc12le5a60s2 series, respectively. when the vcc power drops down to the lvd voltage, the low voltage flag, lvd bit (pcon.5), will be set by hardware. (note that during power-up, this flag will also be set, and the user should clear it by software for the following low voltage detecting.) this flag can also generate an interrupt if bit elvd (ie.6) is set to 1. if external low voltage detection interrupt function is needed to continue normal operation during stop/power- down mode, which can be used to wake up mcu from stop/power-down mode. typical application circuit, using p4.6/rst2/ex_lvd pin achieve low-voltage-detection function, as shown below. 470f + vcc mcu p4.6/lvd gnd 7805 + 104 r1 r2 if power inputing source is 220v ac, then 7805 output is 11v dc. if power inputing source drop down 160v ac 7805 output 8.5v dc and use r1 and r2 can achieve the low voltage detection function at 1.33v ) similarly, if p4.6/rst2/ex_lvd is used to external low voltage detection function, stc-isp writter/programmer should be configured as shown in following figure. user can configure p4.6 as external low voltage dection function through setting this option stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 51 www.stcmcu.com ie: interrupt enable rsgister sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ie a8h name ea elvd eadc es et1 ex1 et0 ex0 enable bit = 1 enables the interrupt . enable bit = 0 disables it . ea (ie.7): disables all interrupts. if ea = 0,no interrupt will be acknowledged. if ea = 1, each interrupt source is individually enabled or disabled by setting or clearing its enable bit. elvd (ie.6): low volatge detection interrupt enable bit. iph: interrupt priority high register sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 iph b7h name ppcah plvdh padch psh pt1h px1h pt0h px0h ip: interrupt priority register sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ie b8h name ppca plvd padc ps pt1 px1 pt0 px0 plvdh, plvd: low voltage detection interrupt priority control bits. if plvdh=0 and plvd=0, low voltage detection interrupt is assigned lowest priority(priority 0). if plvdh=0 and plvd=1, low voltage detection interrupt is assigned lower priority(priority 1). if plvdh=1 and plvd=0, low voltage detection interrupt is assigned higher priority(priority 2). if plvdh=1 and plvd=1,low voltage detection interrupt is assigned highest priority(priority 3). pcon register (power control register) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 pcon 87h name smod smod0 lvdf pof gf1 gf0 pd idl lvdf : pin low-voltage flag. once low voltage condition is detected (vcc power is lower than lvd voltage), it is set by hardware (and should be cleared by software). mnemonic add name b7 b6 b5 b4 b3 b2 b1 b0 reset value pcon 87h power control smod smod0 lvdf pof gf1 gf0 pd idl 0011,0000 wake_clko 8fh clk_output powerdown_wakeup control register pcawakeup rxd_pin_ie t1_pin_ie t0_pin_ie lvd_wake brtclko t1clko t0clko 0000,0000 ie a8h interrupt enable ea elvd eadc es et1 ex1 et0 ex0 0000,0000 ip b8h interrupt priority low ppca plvd padc ps pt1 px1 pt0 px0 0000,0000 iph b7h interrupt priority high ppcah plvdh padch psh pt1h px1h pt0h px0h 0000,0000 some sfrs related to low voltage detection as shown below. wake_clko register bit b7 b6 b5 b4 b3 b2 b1 b0 name pcawakeup rxd_pin_ie t1_pin_ie t0_pin_ie lvd_wake brtcklo t1cklo t0cklo lvd_wake :when set and the associated-lvd interrupt control registers is configured correctly, the cmpin pin is enabled to wake up mcu from power-down state. brtcklo : when set, p1.0 is enabled to be the clock output of baud-rate timer (brt). the clock rate is brg overflow rate divided by 2. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 52 www.stcmcu.com the program that demostrates the external low voltage detection function on p4.6 as shown below: ;/*---------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited ------------------------------------*/ ;/* --- stc 1t series mcu external low voltage detection demo ---*/ ;/* --- mobile: (86)13922809991 --------------------------------------------*/ ;/* --- fax: 86-755-82905966 ------------------------------------------------*/ ;/* --- tel: 86-755-82948412 -------------------------------------------------*/ ;/* --- web: www.stcmcu.com -------------------------------------------*/ ;/* if you want to use the program or the program referenced in the --*/ ;/* article, please specify in which data and procedures from stc --*/ ;/*---------------------------------------------------------------------------------*/ run_led equ p1.0 ;program normal running led indicator error_led equ p1.1 ;error led indicator hi_volt_led equ p1.2 ;normal voltage led indicator power_on_led equ p1.3 ;power-on led indicator low_volt_led equ p1.4 ;low-voltage led indiactor org 0000h ajmp main org 0100h main: mov sp, #070h ;initialize stack pointer setb run_led ;demo program start to work lcall delay ;delay clr run_led ;demo program start to work lcall delay ;delay setb run_led ;demo program start to work main1: mov a, pcon jbc acc.5, power_on_1 clr error_led setb power_on_led setb hi_volt_led setb low_volt_led error: ljmp error power_on_1: setb error_led clr power_on_led setb hi_volt_led setb low_volt_led lcall delay ;delay stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 53 www.stcmcu.com continue_read: mov a, #11011111b anl pcon, a nop mov a, pcon jbc acc.5, low_voltage high_voltage: setb error_led setb power_on_led clr hi_volt_led setb low_volt_led ljmp continue_read low_voltage: setb error_led setb power_on_led clr hi_volt_led setb low_volt_led ljmp continue_read delay: clr a mov r0, a mov r1, a mov r2, #30h delay_loop: djnz r0, delay_loop djnz r1, delay_loop djnz r2, delay_loop ret end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 54 www.stcmcu.com 2.3.4 software reset writing an 1 to swrst bit in iap_contr register will generate a internal reset. iap_contr: isp/iap control register sfr name sfr address bit b7 b6 b5 b4 b3 b2 b1 b0 iap_contr c7h name iapen swbs swrst cmd_fail - wt2 wt1 wt0 iapen : isp/iap operation enable. 0 : global disable all isp/iap program/erase/read function. 1 : enable isp/iap program/erase/read function. swbs: software boot selection control bit 0 : boot from main-memory after reset. 1 : boot from isp memory after reset. swrst: software reset trigger control. 0 : no operation 1 : generate software system reset. it will be cleared by hardware automatically. cmd_fail: command fail indication for isp/iap operation. 0 : the last isp/iap command has finished successfully. 1 : the last isp/iap command fails. it could be caused since the access of flash memory was inhibited. ;software reset from user appliction program area (ap area) and switch to ap area to run program mov iap_contr, #00100000b ;swbs = 0(select ap area), swrst = 1(software reset) ;software reset from system isp monitor program area (isp area) and switch to ap area to run program mov iap_contr, #00100000b ;swbs = 0(select ap area), swrst = 1(software reset) ;software reset from user appliction program area (ap area) and switch to isp area to run program mov iap_contr, #01100000b ;swbs = 1(select isp area), swrst = 1(software reset) ;software reset from system isp monitor program area (isp area) and switch to isp area to run program mov iap_contr, #01100000b ;swbs = 1(select isp area), swrst = 1(software reset) this reset is to reset the whole system, all special function registers and i/o prots will be reset to the initial value 2.3.5 power-on reset (por) when vcc drops below the detection threshold of por circuit, all of the logic circuits are reset. when vcc goes back up again, an internal reset is released automatically after a delay of 32768 clocks. the nominal por detection threshold is around 1.9v for 3v device and 3.3v for 5v device. the power-on flag, pof/pcon.4, is set by hardware to denote the vcc power has ever been less than the por voltage. and, it helps users to check if the start of running of the cpu is from power-on or from hardware reset (rst-pin reset), software reset or watchdog timer reset. the pof bit should be cleared by software. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 55 www.stcmcu.com 2.3.5 max810 power-on-reset delay there is another on-chip por delay circuit s integrated on stc12c5a60s2. this circuit is max810sepcial reset circuit and is controlled by configuring stc-isp writter/programmer shown in the next figure. max810 special reset circuit just add 200ms extra reset-delay-time after power-up reset. so it is another power-on reset. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 56 www.stcmcu.com wdt_contr: watch-dog-timer control register sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 wdt_contr 0c1h name wdt_flag - en_wdt clr_wdt idle_wdt ps2 ps1 ps0 wdt_flag : wdt reset flag. 0 : this bit should be cleared by software. 1 : when wdt overflows, this bit is set by hardware to indicate a wdt reset happened. en_wdt : enable wdt bit. when set, wdt is started. clr_wdt : wdt clear bit. when set, wdt will recount. hardware will automatically clear this bit. idle_wdt : wdt idle mode bit. when set, wdt is enabled in idle mode. when clear, wdt is disabled in idle. ps2, ps1, ps0 : wdt pre-scale value set bit. pre-scale value of watchdog timer is shown as the bellowed table : ps2 ps1 ps0 pre-scale wdt overflow time @20mhz 0 0 0 2 39.3 ms 0 0 1 4 78.6 ms 0 1 0 8 157.3 ms 0 1 1 16 314.6 ms 1 0 0 32 629.1 ms 1 0 1 64 1.25 s 1 1 0 128 2.5 s 1 1 1 256 5 s 2.3.3 watch-dog-timer the watch dog timer in stc12c5a60s2 consists of an 8-bit pre-scaler timer and an 15-bit timer. the timer is one-time enabled by setting en_wdt(wdt_contr.5). clearing en_wdt can stop wdt counting. when the wdt is enabled, software should always reset the timer by writing 1 to clr_wdt bit before the wdt overflows. if stc12c5a60s2 series mcu is out of control by any disturbance, that means the cpu can not run the software normally, then wdt may miss the "writting 1 to clr_wdt" and overflow will come. an overflow of watch-dog-timer will generate a internal reset. 1/256 1/128 1/64 1/32 1/16 1/8 1/4 1/2 8-bit prescalar 15-bit timer wdt_flag - en_wdt clr_wdt idle_wdt ps2 ps1 ps0 sysclk/12 idl/pcon.0 wdt_contr wdt structure wdt reset stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 57 www.stcmcu.com wdt overflow time is shown as the bellowed table when sysclk is 11.0592mhz: ps2 ps1 ps0 pre-scale wdt overflow time @11.0592mhz 0 0 0 2 71.1 ms 0 0 1 4 142.2 ms 0 1 0 8 284.4 ms 0 1 1 16 568.8 ms 1 0 0 32 1.1377 s 1 0 1 64 2.2755 s 1 1 0 128 4.5511 s 1 1 1 256 9.1022 s options related with wdt in stc-isp writter/programmer is shown in the following figure the wdt overflow time is determined by the following equation: wdt overflow time = (12 pre-scale 32768) / sysclk the sysclk is 20mhz in the table above. if sysclk is 12mhz, the wdt overflow time is : wdt overflow time = (12 pre-scale 32768) / 12000000 = pre-scale 393216 / 12000000 wdt overflow time is shown as the bellowed table when sysclk is 12mhz: ps2 ps1 ps0 pre-scale wdt overflow time @12mhz 0 0 0 2 65.5 ms 0 0 1 4 131.0 ms 0 1 0 8 262.1 ms 0 1 1 16 524.2 ms 1 0 0 32 1.0485 s 1 0 1 64 2.0971 s 1 1 0 128 4.1943 s 1 1 1 256 8.3886 s stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 58 www.stcmcu.com the following example is a assembly language program that demostrates stc 1t series mcu wdt. ;/*-------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited ----------------------------------*/ ;/* --- stc 1t series mcu wdt demo ---------------------------------*/ ;/* --- mobile: (86)13922809991 ------------------------------------------*/ ;/* --- fax: 86-755-82905966 ----------------------------------------------*/ ;/* --- tel: 86-755-82948412 -----------------------------------------------*/ ;/* --- web: www.stcmcu.com -----------------------------------------*/ ;/* if you want to use the program or the program referenced in the */ ;/* article, please specify in which data and procedures from stc */ ;/*-------------------------------------------------------------------------------*/ ; wdt overflow time = (12 pre-scale 32768) / sysclk wdt_contr equ 0c1h ;wdt address wdt_time_led equ p1.5 ;wdt overflow time led on p1.5 ;the wdt overflow time may be measured by the led light time wdt_flag_led equ p1.7 ;wdt overflow reset flag led indicator on p1.7 last_wdt_time_led_status equ 00h ;bit variable used to save the last stauts of wdt overflow time led indicator ;wdt reset time , the sysclk is 18.432mhz ;pre_scale_word equ 00111100 b ;open wdt, pre-scale value is 32, wdt overflow time=0.68s ;pre_scale_word equ 00111101 b ;open wdt, pre-scale value is 64, wdt overflow time=1.36s ;pre_scale_word equ 00111110 b ;open wdt, pre-scale value is 128, wdt overflow time=2.72s ;pre_scale_word equ 00111111 b ;open wdt, pre-scale value is 256, wdt overflow time=5.44s org 0000h ajmp main org 0100h main: mov a, wdt_contr ;detection if wdt reset anl a, #10000000b jnz wdt_reset ;wdt_contr.7=1, wdt reset, jump wdt reset subroutine ;wdt_contr.7=0, power-on reset, cold start-up, the content of ram is random setb last_wdt_time_led_status ;power-on reset clr wdt_time_led ;power-on reset,open wdt overflow time led mov wdt_contr, #pre_scale_word ;open wdt stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 59 www.stcmcu.com wait1: sjmp wait1 ;wait wdt overflow reset ;wdt_contr.7=1, wdt reset, hot strart-up, the content of ram is constant and just like before reset wdt_reset: clr wdt_flag_led ;wdt reset,open wdt overflow reset flag led indicator jb last_wdt_time_led_status, power_off_wdt_time_led ;when set last_wdt_time_led_status, close the corresponding led indicator ;clear, open the corresponding led indicator ;set wdt_time_led according to the last status of wdt overflow time led indicator clr wdt_time_led ;close the wdt overflow time led indicator cpl last_wdt_time_led_statu ;reverse the last status of wdt overflow time led indicator wait2: sjmp wait2 ;wait wdt overflow reset power_off_wdt_time_led: setb wdt_time_led ;close the wdt overflow time led indicator cpl last_wdt_time_led_status ;reverse the last status of wdt overflow time led indicator wait3: sjmp wait3 ;wait wdt overflow reset end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 60 www.stcmcu.com reset type reset source result warm boot watchdog system will reset to ap address 0000h and begin running user application program reset pin 20h iap_contr 60h iap_contr system will reset to isp address 0000h and begin running isp monitor program, if not detected legitimate isp command, system will software reset to the user program area automatically. cold boot power-on 2.3.8 warm boot and cold boot reset stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 61 www.stcmcu.com chapter 3. memory organization the stc12c5a60s2 series mcu has separate address space for program memory and data memory. the logical separation of program and data memory allows the data memory to be accessed by 8-bit addresses, which can be quickly stored and manipulated by the cpu. program memory (rom) can only be read, not written to. in the stc 12c5a60s2 series, all the program memory are on-chip flash memory, and without the capability of accessing external program memory because of no ex - ternal access enable (/ea) and program store enable (/psen) signals designed. data memory occupies a separate address space from program memory. in the 12c5a60s2 series, there are 256 bytes of internal scratch-pad ram and 1024 bytes of on-chip expanded ram(xram). besides 64k bytes external expanded ram also can be accessed. 3.1 program memory program memory is the memory which stores the program codes for the cpu to execute. there is 8/16/20/32/40/4 8/52/56/62k-bytes of flash memory embedded for program and data storage. the design allows users to configure it as like there are three individual partition banks inside. they are called ap(application program) region, iap (in-application-program) region and isp (in-system-program) boot region. ap region is the space that user program is resided. iap(in-application-program) region is the nonvolatile data storage space that may be used to save important parameters by ap program. in other words, the iap capability of stc12c5a60s2 provides the user to read/write the user-defined on-chip data flash region to save the needing in use of external eeprom device. isp boot region is the space that allows a specific program we calls isp program is resided. inside the isp region, the user can also enable read/write access to a small memory space to store parameters for specific purposes. generally, the purpose of isp program is to fulfill ap program upgrade without the need to remove the device from system. stc12c5a60s2 hardware catches the configuration information since power-up duration and performs out-of-space hardware-protection depending on pre-determined criteria. the criteria is ap region can be accessed by isp program only, iap region can be accessed by isp program and ap program, and isp region is prohibited access from ap program and isp program itself. but if the isp data flash is enabled, isp program can read/write this space. when wrong settings on isp-iap sfrs are done, the out-of-space happens and stc12c5a60s2 follows the criteria above, ignore the trigger command. after reset, the cpu begins execution from the location 0000h of program memory, where should be the starting of the users application code. to service the interrupts, the interrupt service locations (called interrupt vectors) should be located in the program memory. each interrupt is assigned a fixed location in the program memory. the interrupt causes the cpu to jump to that location, where it commences execution of the service routine. external interrupt 0, for example, is assigned to location 0003h. if external interrupt 0 is going to be used, its service routine must begin at location 0003h. if the interrupt is not going to be used, its service location is available as general purpose program memory. the interrupt service locations are spaced at an interval of 8 bytes: 0003h for external interrupt 0, 000bh for timer 0, 0013h for external interrupt 1, 001bh for timer 1, etc. if an interrupt service routine is short enough (as is often the case in control applications), it can reside entirely within that 8-byte interval. longer service routines can use a jump instruction to skip over subsequent interrupt locations, if other interrupts are in use. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 62 www.stcmcu.com 3.2 data memory 3.2.1 on-chip scratch-pad ram just the same as the conventional 8051 micro-controller, there are 256 bytes of sram data memory plus 128 bytes of sfr space available on the stc12c5a60s2. the lower 128 bytes of data memory may be accessed through both direct and indirect addressing. the upper 128 bytes of data memory and the 128 bytes of sfr space share the same address space. the upper 128 bytes of data memory may only be accessed using indirect addressing. the 128 bytes of sfr can only be accessed through direct addressing. the lowest 32 bytes of data memory are grouped into 4 banks of 8 registers each. program instructions call out these registers as r0 through r7. the rs0 and rs1 bits in psw register select which register bank is in use. instructions using register addressing will only access the currently specified bank. this allows more efficient use of code space, since register instructions are shorter than instructions that use direct addressing. the next 16 bytes (20h~2fh) above the register banks form a block of bit-addressable memory space. the 80c51 instruction set includes a wide selection of single-bit instructions, and the 128 bits in this area can be directly addressed by these instructions. the bit addresses in this area are 00h through 7fh. all of the bytes in the lower 128 can be accessed by either direct or indirect addressing while the upper 128 can only be accessed by indirect addressing. sfrs include the port latches, timers, peripheral controls, etc. these registers can only be accessed by direct addressing. sixteen addresses in sfr space are both byte- and bit- addressable. the bit-addressable sfrs are those whose address ends in 0h or 8h. 3fffh 0000h 16k program flash memory stc12c5a16s2 program memory ff 80 special function registers (sfrs) 7f 00 low 128 bytes internal ram high 128 bytes internal ram on-chip scratch-pad ram bank 0 bank 1 bank 2 bank 3 bit addressable 07h 0fh 17h 1fh 00h 08h 10h 18h 20h 30h 2fh 7fh lower 128 bytes of internal sram type program memory stc12c/le5a08s2/ad 0000h~1fffh (8k) stc12c/le5a16s2/ad 0000h~3fffh (16k) stc12c/le5a20s2/ad 0000h~4fffh (20k) stc12c/le5a32s2/ad 0000h~7fffh (32k) stc12c/le5a40s2/ad 0000h~9fffh (40k) stc12c/le5a48s2/ad 0000h~0bfffh (48k) stc12c/le5a52s2/ad 0000h~0cfffh (52k) stc12c/le5a56s2/ad 0000h~0dfffh (56k) stc12c/le5a60s2/ad 0000h~0efffh (60k) iap12c/le5a62s2/ad 0000h~0f7ffh (62k) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 63 www.stcmcu.com psw register sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 psw d0h name cy ac f0 rs1 rs0 ov f1 p cy : carry flag. this bit is set when the last arithmetic operation resulted in a carry (addition) or a borrow (subtrac-tion). it is cleared to logic 0 by all other arithmetic operations. ac : auxilliary carry flag.(for bcd operations) this bit is set when the last arithmetic operation resulted in a carry into (addition) or a borrow from (subtraction) the high order nibble. it is cleared to logic 0 by all other arithmetic operations f0 : flag 0.(available to the user for general purposes) rs1: register bank select control bit 1. rs0: register bank select control bit 0. [rs1 rs0] select which register bank is used during register accesses rs1 rs0 working register bank (r0~r7) and address 0 0 bank 0(00h~07h) 0 1 bank 1(08h~0fh) 1 0 bank 2(10h~17h) 1 1 bank 3(18h~1fh) ov : overflow flag. this bit is set to 1 under the following circumstances: ? an add, addc, or subb instruction causes a sign-change overflow. ? a mul instruction results in an overflow (result is greater than 255). ? a div instruction causes a divide-by-zero condition. the ov bit is cleared to 0 by the add, addc, subb, mul, and div instructions in all other cases. f1 : flag 1. user-defined flag. p : parity flag. this bit is set to logic 1 if the sum of the eight bits in the accumulator is odd and cleared if the sum is even. sp : stack pointer. the stsek pointer register is 8 bits wide. it is incremented before data is stored during push and call executions. the stack may reside anywhere in on-chip ram.on reset, the stack pointer is initialized to 07h causing the stack to begin at location 08h , which is also the first register (r0) of register bank 1. thus, if more than one register bank is to be used, the sp should be initialized to a location in the data memory not being used for data storage. the stack depth can extend up to 256 bytes. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 64 www.stcmcu.com 1024 bytes expanded ram 03ff 0000 auxiliary ram 3.2.2 auxiliary ram there are 1024 bytes of additional data ram available on stc12c5a60s2. they may be accessed by the instructions movx @ri or movx @dptr. a control bit C extram located in auxr.1 register is to control access of auxiliary ram. when set, disable the access of auxiliary ram. when clear (extram=0), this auxiliary ram is the default target for the address range from 0x0000 to 0x03ffand can be indirectly accessed by move external instruction, movx @ri and movx @dptr. if extram=0 and the target address is over 0x03ff, switches to access external ram automatically. when extram=0, the content in dph is ignored when the instruction movx @ri is executed. for keil-c51 compiler, to assign the variables to be located at auxiliary ram, the pdata or xdata definition should be used. after being compiled, the variables declared by pdata and xdata will become the memories accessed by movx @ri and movx @dptr, respectively. thus the stc12c5a60s2 hardware can access them correctly. auxr register mnemonic add name 7 6 5 4 3 2 1 0 reset value auxr 8eh auxiliary register t0x12 t1x12 uar_m0x6 brtr s2sm0d brtx12 extram s1brs 0000,0000 t0x12 : timer 0 clock source bit. 0 : the clock source of timer 0 is sysclk/12. it will compatible to the traditional 80c51 mcu 1 : the clock source of timer 0 is sysclk/1. it will drive the t0 faster than a traditional 80c51 mcu t1x12 : timer 1 clock source bit. 0 : the clock source of timer 1 is sysclk/12. it will compatible to the traditional 80c51 mcu 1 : the clock source of timer 1 is sysclk/1. it will drive the t0 faster than a traditional 80c51 mcu uart_m0x6 : baud rate select bit of uart1 while it is working under mode-0 0 : the baud-rate of uart in mode 0 is sysclk/12. 1 : the baud-rate of uart in mode 0 is sysclk/2. brtr : dedicated baud-rate timer run control bit. 0 : the baud-rate generator is stopped. 1 : the baud-rate generator is enabled. s2smod : the baud-rate of uart2 double contol bit. 0 : default. the baud-rate of uart2 (s2) is not doubled. 1 : the baud-rate uart2 (s2) is doubled. brtx12 : dedicated baud-rate timer counter control bit. 0 : the baud-rate generator is incremented every 12 system clocks. 1 : the baud-rate generator is incremented every system clock. ffff 0000 64k bytes off-chip expanded ram external ram stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 65 www.stcmcu.com extram : internal / external ram access control bit. 0 : on-chip auxiliary ram is enabled and located at the address 0x0000 to 0x03ff. for address over 0x03ff, off-chip expanded ram becomes the target automatically. 1 : on-chip auxiliary ram is always disabled. s1brs : the baud-rate generator of uart1 select bit. 0 : default. select timer 1 as the baud-rate generator of uart1 1 : timer 1 is replaced by the independent baud-rate generator which is selected as the baud-rate of uart. in other words, timer 1 is released to use in other functions. off-chip expanded ram 63kb auxiliary ram 1kb 0x0000 0x03ff 0x0400 0xffff off-chip expanded ram 64kb ffffh extram=0 extram=1 0000h stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 66 www.stcmcu.com an example program for internal expanded ram demo of stc12c5a60s2: ;/*--------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited -----------------------------------*/ ;/* --- stc 1t series mcu internal expanded ram demo -----------*/ ;/* --- mobile: (86)13922809991 ------------------------------------------*/ ;/* --- fax: 86-755-82905966 ----------------------------------------------*/ ;/* --- tel: 86-755-82948412 -----------------------------------------------*/ ;/* --- web: www.stcmcu.com ------------------------------------------*/ ;/* if you want to use the program or the program referenced in the */ ;/* article, please specify in which data and procedures from stc */ ;/*-------------------------------------------------------------------------------*/ #include #include /* use _nop_( ) function */ sfr auxr = 0x8e; sbit errom_led = p1^5; sbit ok_led = p1^7; void main ( ) { unsigned int array_point = 0; /*test-array: test_array_one[512], test_array_two[512] */ unsigned char xdata test_array_one[512] = { 0x00, 0x01 0x02, 0x03, 0x04 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 67 www.stcmcu.com 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb ,0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0, 0xef, 0xee, 0xed, 0xec, 0xeb, 0xea, 0xe9, 0xe8, 0xe7, 0xe6, 0xe5, 0xe4, 0xe3, 0xe2, 0xe1, 0xe0, 0xdf, 0xde, 0xdd, 0xdc, 0xdb, 0xda, 0xd9, 0xd8, 0xd7, 0xd6, 0xd5, 0xd4, 0xd3, 0xd2, 0xd1, 0xd0, 0xcf, 0xce, 0xcd, 0xcc, 0xcb, 0xca, 0xc9, 0xc8, 0xc7, 0xc6, 0xc5, 0xc4, 0xc3, 0xc2, 0xc1, 0xc0, 0xbf, 0xbe, 0xbd, 0xbc, 0xbb, 0xba, 0xb9, 0xb8, 0xb7, 0xb6, 0xb5, 0xb4, 0xb3, 0xb2, 0xb1, 0xb0, 0xaf, 0xae, 0xad, 0xac, 0xab, 0xaa, 0xa9, 0xa8, 0xa7, 0xa6, 0xa5, 0xa4, 0xa3, 0xa2, 0xa1, 0xa0, 0x9f, 0x9e, 0x9d, 0x9c, 0x9b, 0x9a, 0x99, 0x98, 0x97, 0x96, 0x95, 0x94, 0x93, 0x92, 0x91, 0x90, 0x8f, 0x8e, 0x8d, 0x8c, 0x8b, 0x8a, 0x89, 0x88, 0x87, 0x86, 0x85, 0x84, 0x83, 0x82, 0x81, 0x80, 0x7f, 0x7e, 0x7d, 0x7c, 0x7b, 0x7a, 0x79, 0x78, 0x77, 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x70, 0x6f, 0x6e, 0x6d, 0x6c, 0x6b, 0x6a, 0x69, 0x68, 0x67, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x60, 0x5f, 0x5e, 0x5d, 0x5c, 0x5b, 0x5a, 0x59, 0x58, 0x57, 0x56, 0x55, 0x54, 0x53, 0x52, 0x51, 0x50, 0x4f, 0x4e, 0x4d, 0x4c, 0x4b, 0x4a, 0x49, 0x48, 0x47, 0x46, 0x45, 0x44, 0x43, 0x42, 0x41, 0x40, 0x3f, 0x3e, 0x3d, 0x3c, 0x3b, 0x3a, 0x39, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x30, 0x2f, 0x2e, 0x2d, 0x2c, 0x2b, 0x2a, 0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21, 0x20, 0x1f, 0x1e, 0x1d, 0x1c, 0x1b, 0x1a, 0x19, 0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 }; unsigned char xdata test_array_two[512] = { 0x00, 0x01 0x02, 0x03, 0x04 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 68 www.stcmcu.com 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb ,0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0, 0xef, 0xee, 0xed, 0xec, 0xeb, 0xea, 0xe9, 0xe8, 0xe7, 0xe6, 0xe5, 0xe4, 0xe3, 0xe2, 0xe1, 0xe0, 0xdf, 0xde, 0xdd, 0xdc, 0xdb, 0xda, 0xd9, 0xd8, 0xd7, 0xd6, 0xd5, 0xd4, 0xd3, 0xd2, 0xd1, 0xd0, 0xcf, 0xce, 0xcd, 0xcc, 0xcb, 0xca, 0xc9, 0xc8, 0xc7, 0xc6, 0xc5, 0xc4, 0xc3, 0xc2, 0xc1, 0xc0, 0xbf, 0xbe, 0xbd, 0xbc, 0xbb, 0xba, 0xb9, 0xb8, 0xb7, 0xb6, 0xb5, 0xb4, 0xb3, 0xb2, 0xb1, 0xb0, 0xaf, 0xae, 0xad, 0xac, 0xab, 0xaa, 0xa9, 0xa8, 0xa7, 0xa6, 0xa5, 0xa4, 0xa3, 0xa2, 0xa1, 0xa0, 0x9f, 0x9e, 0x9d, 0x9c, 0x9b, 0x9a, 0x99, 0x98, 0x97, 0x96, 0x95, 0x94, 0x93, 0x92, 0x91, 0x90, 0x8f, 0x8e, 0x8d, 0x8c, 0x8b, 0x8a, 0x89, 0x88, 0x87, 0x86, 0x85, 0x84, 0x83, 0x82, 0x81, 0x80, 0x7f, 0x7e, 0x7d, 0x7c, 0x7b, 0x7a, 0x79, 0x78, 0x77, 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x70, stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 69 www.stcmcu.com 0x6f, 0x6e, 0x6d, 0x6c, 0x6b, 0x6a, 0x69, 0x68, 0x67, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x60, 0x5f, 0x5e, 0x5d, 0x5c, 0x5b, 0x5a, 0x59, 0x58, 0x57, 0x56, 0x55, 0x54, 0x53, 0x52, 0x51, 0x50, 0x4f, 0x4e, 0x4d, 0x4c, 0x4b, 0x4a, 0x49, 0x48, 0x47, 0x46, 0x45, 0x44, 0x43, 0x42, 0x41, 0x40, 0x3f, 0x3e, 0x3d, 0x3c, 0x3b, 0x3a, 0x39, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x30, 0x2f, 0x2e, 0x2d, 0x2c, 0x2b, 0x2a, 0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21, 0x20, 0x1f, 0x1e, 0x1d, 0x1c, 0x1b, 0x1a, 0x19, 0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 }; error_led = 1; ok_led = 1; for (array_point = 0; array_point<512; array_point++) { if (test_array_one[array_point] != test_array_two [array_point]) { error_led = 0; ok_led = 1; break; } else{ ok_led = 0; error_led = 1; } } while (1); } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 70 www.stcmcu.com 3.2.3 external expandable 64kb ram (off-chip ram) there is 64k-byte addressing space available for stc12c5a60s2 to access external data ram. just the same as the design in the conventional 8051, the port C p2, p0, ale, p3.6 and p3.7 have alterative function for external data ram access. in addition, a new register bus_speed (address: 0xa1) is design to control the acess timimg of "movx" instruction. in bus_speed register, {ales1 and ales0} is to stretch the setup time and hold time with respect to ale negative edge and {rw2, rw1, rw0} is to stretch the pulse width of /wr(p3.6) and /rd(p3.7). by using bus_speed to change the instruction cycle time, stc12c5a60s2 can conformed to communicate with both of fast and slow peripheral devices without loss of communication efficiency. bus_speed register mnemonic add name b7 b6 b5 b4 b3 b2 b1 b0 reset value bus_speed a1h bus-speed control - - ales1 ales0 - rws2 rws1 rws0 xx10,x011 ales1 ales0 0 0 the p0 address setup time and hold time to ale negative edge is one clock cycle 0 1 the p0 address setup time and hold time to ale negative edge is two clock cycles. 1 0 the p0 address setup time and hold time to ale negative edge is three clock cycles. (default) 1 1 the p0 address setup time and hold time to ale negative edge is four clock cycles. rws2 rws1 rws0 0 0 0 the movx read/write pulse is 1 clock cycle. 0 0 1 the movx read/write pulse is 2 clock cycles. 0 1 0 the movx read/write pulse is 3 clock cycles. 0 1 1 the movx read/write pulse is 4 clock cycles. (default) 1 0 0 the movx read/write pulse is 5 clock cycles. 1 0 1 the movx read/write pulse is 6 clock cycles. 1 1 0 the movx read/write pulse is 7 clock cycles 1 1 1 the movx read/write pulse is 8 clock cycles when the target is on-chip auxiliary ram, the setting on bus_speed register is discarded by hardware . mnemonic description byte execution clocks of conventional 8051 execution clocks of stc 1t mcu promoted efficiency movx a, @ri move external ram(8-bit addr) to acc 1 24 3 8x movx @ri, a move acc to external ram(8-bit addr) 1 24 4 6x movx a, @dptr move external ram(16-bit addr) to acc 1 24 3 8x movx @dptr, a move acc to external ram (16-bit addr) 1 24 3 8x movx a, @ri move external ram(8-bit addr) to acc 1 24 7+? *note1 movx @ri, a move acc to external ram(8-bit addr) 1 24 7+? *note1 movx a, @dptr move external ram(16-bit addr) to acc 1 24 7+? *note1 movx @dptr, a move acc to external ram (16-bit addr) 1 24 7+? *note1 note1: the clocks needed by accessing off-chip expanded ram are 7+2 ale_bus_speed+rw_bus_speed ale_bus_speed is controlled by ales1/ales0 bits of register bus_speed rw_bus_speed is controlled by rws2/rws1/rws0 bits of register bus_speed stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 71 www.stcmcu.com timing diagram for movx @dptr, a without stretch 1 2 3 4 5 6 7 1 2 3 4 5 6 7 clock p2 p0 ale /wr (p3.6) high-byte address ff high-byte address weak pullup data for writing low-byte address low-byte address ff data for writing movx write cycle movx write cycle weak pullup 1 2 3 4 5 6 7 1 2 3 4 5 6 7 clock p2 p0 ale /rd (p3.7) high-byte address high-byte address data low-byte address low-byte address data movx read cycle movx read cycle port weak-pullup timing diagram for movx a, @dptr without stretch port weak-pullup 1 2 3 4 5 6 7 8 9 0 1 2 3 4 clock p2 p0 ale /wr (p3.6) high-byte address data for writing low-byte address movx write cycle twr = (1+7) cycles timing diagram for movx @dptr, a with stretch {rws2,rws1,rws0} = 3b111 twr = 8 clock cycles (twr is stretched by 7 cycles). stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 72 www.stcmcu.com timing diagram for movx @dptr, a with stretch {rws2,rws1,rws0} = 3b111 and {ales1,ales0} == 2b11 the trd is stretched by 7, so twr = 8 clock cycles. tales is stretched by 3, so tales = 4 clock cycles and taleh = 4 clock cycles. 1 clock p2 p0 ale /rd (p3.7) high-byte address ff low-byte address ale hold time (1+3) cycles movx read cycle weak-pullup 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 data ff weak-pullup trd = (1+7) cycles ale setup time (1+3) cycles stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 73 www.stcmcu.com 3.3 special function registers 0/8 1/9 2/a 3/b 4/c 5/d 6/e 7/f 0f8h ch ccap0h ccap1h 0ffh 0000,0000 0000,0000 0000,0000 0f0h b pca_pwm0 pca_pwm1 0f7h 0000,0000 xxxx,xx00 xxxx,xx00 0e8h cl ccap0l ccap1l 0efh 0000,0000 0000,0000 0000,0000 0e0h acc 0e7h 0000,0000 0d8h ccon cmod ccapm0 ccapm1 0dfh 00xx,xx00 0xxx,0000 x000,0000 x000,0000 0d0h psw 0d7h 0000,0000 0c8h p5 p5m1 p5m0 spstat spctl spdat 0cfh xxxx,1111 xxxx,0000 xxxx,0000 00xx,xxxx 0000,0100 0000,0000 0c0h p4 wdt_contr iap_data iap_addrh iap_addrl iap_cmd iap_trig iap_contr 0c7h 1111,1111 0x00,0000 1111,1111 0000,0000 0000,0000 xxxx,xx00 xxxx,xxxx 0000,x000 0b8h ip saden p4sw adc_contr adc_res adc_resl 0bfh 0000,0000 0000,0000 x000,xxxx 0000,0000 0000,0000 0000,0000 0b0h p3 p3m1 p3m0 p4m1 p4m0 ip2 ip2h iph 0b7h 1111,1111 0000,0000 0000,0000 0000,0000 0000,0000 xxxx,xx00 xxxx,xx00 0000,0000 0a8h ie saddr ie2 0afh 0000,0000 0000,0000 xxxx,xx00 0a0h p2 bus_speed auxr1 test_wdt 0a7h 11111111 xx10,x011 0000,0000 don't use 098h scon sbuf s2con s2buf brt p1asf 09fh 0000,0000 xxxx,xxxx 0000,0000 xxxx,xxxx 0000,0000 0000,0000 090h p1 p1m1 p1m0 p0m1 p0m0 p2m1 p2m0 clk_div 097h 1111,1111 0000,0000 0000,0000 0000,0000 0000,0000 0000,0000 0000,0000 xxxx,x000 088h tcon tmod tl0 tl1 th0 th1 auxr wake_clko 08fh 00000000 00000000 0000,0000 0000,0000 0000,0000 0000,0000 000 0,0000 0000,0x00 080h p0 sp dpl dph pcon 087h 1111,1111 0000,0111 0000,0000 0000,0000 0011,0000 0/8 1/9 2/a 3/b 4/c 5/d 6/e 7/f bit addressable non bit addressable 3.3.1 special function registers address map stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 74 www.stcmcu.com symbol description address bit address and symbol msb lsb value after power-on or reset p0 port 0 80h p0.7 p0.6 p0.5 p0.4 p0.3 p0.2 p0.1 p0.0 1111 1111b sp stack pointer 81h 0000 0111b dptr dpl dph data pointer low 82h 0000 0000b data pointer high 83h 0000 0000b pcon power control 87h smod smod0 lvdf pof gf1 gf0 pd idl 0011 0000b tcon timer control 88h tf1 tr1 tf0 tr0 ie1 it1 ie0 it0 0000 0000b tmod timer mode 89h gate c/t m1 m0 gate c/t m1 m0 0000 0000b tl0 timer low 0 8ah 0000 0000b tl1 timer low 1 8bh 0000 0000b th0 timer high 0 8ch 0000 0000b th1 timer high 1 8dh 0000 0000b auxr auxiliary register 8eh t0x12 t1x12 uart_m0x6 brtr s2smod brtx12 extram s1brs 0000 0000b wake_clko clk_output power down wake-up control register 8fh pcawakeup rxd_pin_ie t1_pin_ie t0_pin_ie lvd_wake brtclko t1clko t0clko 0000 0000b p1 port 1 90h p1.7 p1.6 p1.5 p1.4 p1.3 p1.2 p1.1 p1.0 1111 1111b p1m1 p1 configuration 1 91h 0000 0000b p1m0 p1 configuration 0 92h 0000 0000b p0m1 p0 configuration 1 93h 0000 0000b p0m0 p0 configuration 0 94h 0000 0000b p2m1 p2 configuration 1 95h 0000 0000b p2m0 p2 configuration 0 96h 0000 0000b clk_div clock divder 97h - - - - - clks2 clks1 clks0 xxxx x000b scon serial control 98h sm0/fe sm1 sm2 ren tb8 rb8 ti ri 0000 0000b sbuf serial buffer 99h xxxx xxxxb s2con s2 control 9ah s2sm0 s2sm1 s2sm2 s2ren s2tb8 s2rb8 s2ti s2ri 0000 0000b s2sbuf s2 serial buffer 9bh xxxx xxxxb brt dedicated baud- rate timer 9ch 0000 0000b p1asf p1 analog special function 9dh p17asf p16asf p15asf p14asf p13asf p12asf p11asf p10asf 0000 0000b p2 port 2 a0h p2.7 p2.6 p2.5 p2.4 p2.3 p2.2 p2.1 p2.0 1111 1111b bus_speed bus-speed control a1h - - ales1 ales0 - rws2 rws1 rws0 xx10 x011b auxr1 auxiliary register1 a2h - pca_p4 spi_p4 s2_p4 gf2 adrj - dps 0000 0000b ie interrupt enable a8h ea elvd eadc es et1 ex1 et0 ex0 0x00 0000b saddr slave address a9h 0000 0000b ie2 interrupt enable 2 afh - - - - - - espi es2 xxxx xx00b 3.3.2 special function registers bits description stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 75 www.stcmcu.com symbol description address bit address and symbol msb lsb value after power-on or reset p3 port 3 b0h p3.7 p3.6 p3.5 p3.4 p3.3 p3.2 p3.1 p3.0 1111 1111b p3m1 p2 configuration 1 b1h 0000 0000b p3m0 p3 configuration 0 b2h 0000 0000b p4m1 p4 configuration 1 b3h 0000 0000b p4m0 p4 configuration 0 b4h 0000 0000b ip2 2rd interrupt priority low register b5h - - - - - - pspi ps2 xxxx xx00b ip2h 2rd interrupt priority low register b6h - - - - - - pspih ps2h xxxx xx00b iph interrupt priority high b7h ppcah plvdh padch psh pt1h px1h pt0h px0h 0000 0000b ip interrupt priority low b8h ppca plvd padc ps pt1 px1 pt0 px0 0000 0000b saden slave address mask b9h 0000 0000b p4sw port 4 switch bbh - lvd_p4.6 ale_p4.5 na_p4.4 - - - - x000 xxxxb adc_contr adc control bch adc_power speed1 speed0 adc_flag adc_start chs2 chs1 chis0 0000 0000b adc_res adc result bdh 0000 0000b adc_resl adc result low beh 0000 0000b p4 port 4 c0h p4.7 p4.6 p4.5 p4.4 p4.3 p4.2 p4.1 p4.0 1111 1111b wdt_contr watch-dog-timer control register c1h wdt_flag - en_wdt clr_wdt idle_wdt ps2 ps1 ps0 xx00 0000b iap_data isp/iap flash data register c2h 1111 1111b iap_addrh isp/iap flash address high c3h 0000 0000b iap_addrl isp/iap flash address low c4h 0000 0000b iap_cmd isp/iap flash command register c5h - - - - - - ms1 ms0 xxxx x000b iap_trig isp/iap flash command trigger c6h xxxx xxxxb iap_contr isp/iap control register c7h iapen swbs swrst cmd_fail - wt2 wt1 wt0 0000 x000b p5 port 5 c8h - - - - p5.3 p5.2 p5.1 p5.0 xxxx 1111b p5m1 p5 configuration 1 c9h 0000 0000b p5m0 p5 configuration 0 cah 0000 0000b spstat spi status register cdh spif wcol - - - - - - 00xx xxxxb spctl spi control register ceh ssig spen dord mstr cpol cpha spr1 spr0 0000 0100b spdat spi data register cfh - - - - - - - - 0000 0000b stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 76 www.stcmcu.com symbol description address bit address and symbol msb lsb value after power-on or reset psw program status word d0h cy ac f0 rs1 rs0 ov f1 p 0000 0000b ccon pca control register d8h cf cr - - - - ccf1 ccf0 00xx xx00b cmod pca mode register d9h cidl - - - cps2 cps1 cps0 ecf 00xx 0000b ccapm0 pca module 0 mode register dah - ecom0 capp0 capn0 mat0 tog0 pwm0 eccf0 x000 0000b ccapm1 pca module 1 mode register dbh - ecom1 capp1 capn1 mat1 tog1 pwm1 eccf1 x000 0000b acc accumulator e0h 0000 0000b cl pca base timer low e9h 0000 0000b ccap0l pca module 0 capture register low eah 0000 0000b ccap1l pca module 1 capture register low ebh 0000 0000b b b register f0h 0000 0000b pca_pwm0 pca pwm mode auxiliary register 1 f2h - - - - - - epc0h epc0l xxxx xx00b pca_pwm1 pca pwm mode auxiliary register 1 f3h - - - - - - epc1h epc1l xxxx xx00b ch pca base timer high f9h 0000 0000b ccap0h pca module 0 capture register high fah 0000 0000b ccap1h pca module 1 capture register high fbh 0000 0000b accumulator acc is the accumulator register. the mnemonics for accumulator-specific instructions, however, refer to the accumulator simply as a. b-register the b register is used during multiply and divide operations. for other instructions it can be treated as another scratch pad register. stack pointer the stack pointer register is 8 bits wide. it is incrementde before data is stored during push and call executions. while the stack may reside anywhee in on-chip ram, the stack pointer is initialized to 07h after a reset. therefore, the first value pushed on the stack is placed at location 0x08, which is also the first register (r0) of register bank 1. thus, if more than one register bank is to be used, the sp should be initialized to a location in the data memory not being used for data storage. the stack depth can extend up to 256 bytes. some common sfrs of standard 8051 are shown as below. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 77 www.stcmcu.com program status word(psw) the program status word(psw) contains several status bits that reflect the current state of the cpu. the psw, shown below, resides in the sfr space. it contains the carry bit, the auxiliary carry(for bcd operation), the two register bank select bits, the overflow flag, a parity bit and two user-definable status flags. the carry bit, other than serving the function of a carry bit in arithmetic operations, also serves as the accumulator for a number of boolean operations. the bits rs0 and rs1 are used to select one of the four register banks shown in the previous page. a number of instructions refer to these ram locations as r0 through r7. the parity bit reflects the number of 1s in the accumulator. p=1 if the accumulator contains an odd number of 1s and otherwise p=0. psw register sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 psw d0h name cy ac f0 rs1 rs0 ov f1 p cy : carry flag. this bit is set when the last arithmetic operation resulted in a carry (addition) or a borrow (subtrac-tion). it is cleared to logic 0 by all other arithmetic operations. ac : auxilliary carry flag.(for bcd operations) this bit is set when the last arithmetic operation resulted in a carry into (addition) or a borrow from (subtraction) the high order nibble. it is cleared to logic 0 by all other arithmetic operations f0 : flag 0.(available to the user for general purposes) rs1: register bank select control bit 1. rs0: register bank select control bit 0. [rs1 rs0] select which register bank is used during register accesses rs1 rs0 working register bank (r0~r7) and address 0 0 bank 0(00h~07h) 0 1 bank 1(08h~0fh) 1 0 bank 2(10h~17h) 1 1 bank 3(18h~1fh) ov : overflow flag. this bit is set to 1 under the following circumstances: ? an add, addc, or subb instruction causes a sign-change overflow. ? a mul instruction results in an overflow (result is greater than 255). ? a div instruction causes a divide-by-zero condition. the ov bit is cleared to 0 by the add, addc, subb, mul, and div instructions in all other cases. f1 : flag 1. user-defined flag. p : parity flag. this bit is set to logic 1 if the sum of the eight bits in the accumulator is odd and cleared if the sum is even. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 78 www.stcmcu.com auxr1 register mnemonic add name 7 6 5 4 3 2 1 0 reset value auxr1 a2h auxiliary register 1 - pca_p4 spi_p4 s2_p4 gf2 adrj - dps x000,00x0 pca _p4 0 : default. the pca function is on p1[4:2] 1 : the pca function on p1[4:2] is switched to p4[3:1]. eci is switched from p1.2 to p4.1 pca0/pwm0 is switched from p1.3 to p4.2 pca1/pwm1 is switched from p1.4 to p4.3 spi_p4 0 : default. the spi function is on p1[7:4] 1 : the spi function on p1[7:4] is switched to p4[3:0]. sclk is switched from p1.7 to p4.3 mosi is switched from p1.6 to p4.2 miso is switched from p1.5 to p4.1 ss is switched from p1.4 to p4.0 s2_p4 0 : default. the uart2(s2) function is on p1[3:2] 1 : the uart2(s2) function on p1[3:2] is switched to p4[3:2]. txd2 is switched from p1.3 to p4.3 rxd2 is switched from p1.2 to p4.2 gf2 : general flag. it can be used by software. adrj 0 : the 10-bit conversion result of adc is arranged as {adc_res[7:0], adc_resl[1:0]}. 1 : the 10-bit conversion result is right-justified, {adc_res[1:0], adc_resl[7:0]}. dps 0 : default. dptr0 is selected as data pointer. 1 : the secondary dptr is switched to use. 3.3.3 dual data pointer register (dptr) the data pointer (dptr) consists of a high byte (dph) and a low byte (dpl). its intended function is to hold a 16-bit address. it may be manipulated as a 16-bit register or as two independent 8-bit registers. for fast data movement, stc12c5a60s2 supports two data pointers. they share the same sfr address and are switched by the register bit C dps/auxr.0. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 79 www.stcmcu.com the following program is an assembly program that demostrates how the dual data pointer be used. ;/*--------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited -----------------------------------*/ ;/* --- stc 1t series mcu dual data pointer demo -------------------*/ ;/* --- mobile: (86)13922809991 ------------------------------------------*/ ;/* --- fax: 86-755-82905966 ----------------------------------------------*/ ;/* --- tel: 86-755-82948412 -----------------------------------------------*/ ;/* --- web: www.stcmcu.com ------------------------------------------*/ ;/* if you want to use the program or the program referenced in the */ ;/* article, please specify in which data and procedures from stc */ ;/*-------------------------------------------------------------------------------*/ auxr1 data 0a2h ;define special function register auxr1 mov auxr1, #0 ;dps=0, select dptr0 mov dptr, #1ffh ;set dptr0 for 1ffh mov a, #55h movx @dptr, a ;load the value 55h in the 1ffh unit mov dptr, #2ffh ;set dptr0 for 2ffh mov a, #0aah movx @dptr, a ;load the value 0aah in the 2ffh unit inc auxr1 ;dps=1, dptr1 is selected mov dptr, #1ffh ;set dptr1 for 1ffh movx a, @dptr ;get the content of 1ffh unit ;which is pointed by dptr1, ;the content of accumulator has changed for 55h inc auxr1 ;dps=0, dptr0 is selected movx a, @dptr ;get the content of 2ffh unit ;which is pointed by dptr0, ;the content of accumulator has changed for 0aah inc auxr1 ;dps=1, dptr1 is selected movx a, @dptr ;get the content of 1ffh unit ;which is pointed by dptr1, ;the content of accumulator has changed for 55h inc auxr1 ;dps=0, dptr0 is selected movx a, @dptr ;get the content of 2ffh unit ;which is pointed by dptr0, ;the content of accumulator has changed for 0aah stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 80 www.stcmcu.com chapter 4. configurable i/o ports of stc12c5a60s2 series 4.1 i/o ports configurations all i/o ports (including p4 and p5) of stc12c5a60s2 may be independently configured to one of four modes by setting the corresponding bit in two mode registers pxmn (x= 0 ~ 5, n = 0, 1).the four modes are quasi- bidirectional (standard 8051 port output), push-pull output, input-only or open-drain output. all port pins default to quasi-bidirectional after reset. each one has a schmitt-triggered input for improved input noise rejection. any port can drive 20ma current, but the whole chip had better drive lower than 120ma current. p4.4, p4.5, p4.6 and p4.7 are located at the pins - psen, ale, ea and rst of conventional 80c51. pay attention that additional control bits on p4sw register are used to enable the i/o port functions of these pins. prior to use them as i/o port, the users must set the corresponding bit to enable it. configure i/o ports mode p5 configure (p 5 address address c8h) ) p5m1 [ 3 : 0 ] p5m0 [ 3 : 0 ] i/o ports mode 0 0 quasi_bidirectional(standard 8051 i/o port output , sink current up to 20ma , pull-up current is 230a , because of manufactured error, the actual pull-up current is 250ua ~ 150ua 0 1 push-pull output (strong pull-up output current can be up to 20ma, resistors need to be added to restrict current 1 0 input-only (high-impedance ) 1 1 open drain internal pull-up resistors should be disabled and external pull-up resistors need to join. example: mov p5m1, #xxxx1010b mov p5m0, #xxxx1100b ;p5.3 in open drain mode, p5.2 in strong push-pull output , p5.1 in high-impedance input, p5.0 in quasi_bidirectional /weak pull-up /weak pull-up p4 configure (p 4 address address c0h) ) p4m1 [ 7 : 0 ] p4m0 [ 7 : 0 ] i/o ports mode 0 0 quasi_bidirectional(standard 8051 i/o port output , sink current up to 20ma , pull-up current is 230a , because of manufactured error, the actual pull-up current is 250ua ~ 150ua 0 1 push-pull output (strong pull-up output current can be up to 20ma, resistors need to be added to restrict current 1 0 input-only (high-impedance ) 1 1 open drain internal pull-up resistors should be disabled and external pull-up resistors need to join. example: mov p4m1, #10100000b mov p4m0, #11000000b ;p4.7 in open drain mode, p4.6 in strong push-pull output , p4.5 in high-impedance input, p4.4/p4.3/p4.2/ p4.1/p4.0 in quasi_bidirectional /weak pull-up /weak pull-up stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 81 www.stcmcu.com p3 configure (p 3 address address b0h) ) p3m1 [ 7 : 0 ] p3m0 [ 7 : 0 ] i/o ports mode 0 0 quasi_bidirectional(standard 8051 i/o port output , sink current up to 20ma , pull-up current is 230a , because of manufactured error, the actual pull-up current is 250ua ~ 150ua 0 1 push-pull output (strong pull-up output current can be up to 20ma, resistors need to be added to restrict current 1 0 input-only (high-impedance ) 1 1 open drain internal pull-up resistors should be disabled and external pull-up resistors need to join. example: mov p3m1, #10100000b mov p3m0, #11000000b ;p3.7 in open drain mode, p3.6 in strong push-pull output , p3.5 in high-impedance input, p3.4/p3.3/p3.2/ p3.1/p3.0 in quasi_bidirectional /weak pull-up /weak pull-up p2m1 [ 7 : 0 ] p2m0 [ 7 : 0 ] i/o ports mode 0 0 quasi_bidirectional(standard 8051 i/o port output , sink current up to 20ma , pull-up current is 230a , because of manufactured error, the actual pull-up current is 250ua ~ 150ua 0 1 push-pull output (strong pull-up output current can be up to 20ma, resistors need to be added to restrict current 1 0 input-only (high-impedance ) 1 1 open drain internal pull-up resistors should be disabled and external pull-up resistors need to join. example: mov p2m1, #10100000b mov p2m0, #11000000b ;p2.7 in open drain mode, p2.6 in strong push-pull output , p2.5 in high-impedance input, p2.4/p2.3/p2.2/ p2.1/p2.0 in quasi_bidirectional /weak pull-up /weak pull-up p1m1 [ 7 : 0 ] p1m0 [ 7 : 0 ] i/o ports mode 0 0 quasi_bidirectional(standard 8051 i/o port output , sink current up to 20ma , pull-up current is 230a , because of manufactured error, the actual pull-up current is 250ua ~ 150ua 0 1 push-pull output (strong pull-up output current can be up to 20ma, resistors need to be added to restrict current 1 0 input-only (high-impedance ) 1 1 open drain internal pull-up resistors should be disabled and external pull-up resistors need to join. example: mov p1m1, #10100000b mov p1m0, #11000000b ;p1.7 in open drain mode, p1.6 in strong push-pull output , p1.5 in high-impedance input, p1.4/p1.3/p1.2/ p1.1/p1.0 in quasi_bidirectional /weak pull-up /weak pull-up p2 configure (p 2 address address a0h) ) p1 configure (p 1 address address 90h) ) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 82 www.stcmcu.com p0m1 [ 7 : 0 ] p0m0 [ 7 : 0 ] i/o ports mode 0 0 quasi_bidirectional (standard 8051 i/o port output , sink current up to 20ma , pull-up current is 230a , because of manufactured error, the actual pull-up current is 250ua ~ 150ua 0 1 push-pull output (strong pull-up output current can be up to 20ma, resistors need to be added to restrict current 1 0 input-only (high-impedance ) 1 1 open drain internal pull-up resistors should be disabled and external pull-up resistors need to join. example: mov p0m1, #10100000b mov p0m0, #11000000b ;p0.7 in open drain mode, p0.6 in strong push-pull output , p0.5 in high-impedance input, p0.4/p0.3/p0.2/ p0.1/p0.0 in quasi_bidirectional /weak pull-up /weak pull-up p0 configure (p 0 address address 80h) ) some sfrs related with i/o ports are listed below. p5 register (bit addressable ) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 p5 c8h name - - - - p5.3 p5.2 p5.1 p5.0 p5m1 register (non bit addressable ) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 p5m1 c9h name - - - - p5m1.3 p5m1.2 p5m1.1 p3m1.0 p5m0 register (non bit addressable ) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 p5m0 cah name - - - - p5m0.3 p5m0.2 p5m0.1 p5m0.0 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 83 www.stcmcu.com p4 register (bit addressable ) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 p4 c0h name p4.7 p4.6 p4.5 p4.4 p4.3 p4.2 p4.1 p4.0 p4 register could be bit-addressable and set/cleared by cpu. and p4.7~p1.0 coulde be set/cleared by cpu. p4.5 is an alternated function on ale pin. p4m1 register (non bit addressable ) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 p4m1 b3h name p4m1.7 p4m1.6 p4m1.5 p4m1.4 p4m1.3 p4m1.2 p4m1.1 p4m1.0 p4m0 register (non bit addressable ) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 p4m0 b4h name p4m0.7 p4m0.6 p4m0.5 p4m0.4 p4m0.3 p4m0.2 p4m0.1 p4m0.0 p3 register (bit addressable ) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 p3 b0h name p3.7 p3.6 p3.5 p3.4 p3.3 p3.2 p3.1 p3.0 p3 register could be bit-addressable and set/cleared by cpu. and p3.7~p3.0 coulde be set/cleared by cpu. p3m1 register (non bit addressable ) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 p3m1 b1h name p3m1.7 p3m1.6 p3m1.5 p3m1.4 p3m1.3 p3m1.2 p3m1.1 p3m1.0 p3m0 register (non bit addressable ) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 p3m0 b2h name p3m0.7 p3m0.6 p3m0.5 p3m0.4 p3m0.3 p3m0.2 p3m0.1 p3m0.0 p2 register (bit addressable ) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 p2 a0h name p2.7 p2.6 p2.5 p2.4 p2.3 p2.2 p2.1 p2.0 p2 register could be bit-addressable and set/cleared by cpu. and p2.7~p2.0 coulde be set/cleared by cpu. p2m1 register (non bit addressable ) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 p2m1 95h name p2m1.7 p2m1.6 p2m1.5 p2m1.4 p2m1.3 p2m1.2 p2m1.1 p2m1.0 p2m0 register (non bit addressable ) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 p2m0 96h name p2m0.7 p2m0.6 p2m0.5 p2m0.4 p2m0.3 p2m0.2 p2m0.1 p2m0.0 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 84 www.stcmcu.com p1 register (bit addressable ) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 p1 90h name p1.7 p1.6 p1.5 p1.4 p1.3 p1.2 p1.1 p1.0 p1 register could be bit-addressable and set/cleared by cpu. and p1.7~p1.0 coulde be set/cleared by cpu. p1m1 register (non bit addressable ) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 p1m1 91h name p1m1.7 p1m1.6 p1m1.5 p1m1.4 p1m1.3 p1m1.2 p1m1.1 p1m1.0 p1m0 register (non bit addressable ) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 p1m0 92h name p1m0.7 p1m0.6 p1m0.5 p1m0.4 p1m0.3 p1m0.2 p1m0.1 p1m0.0 p0 register (bit addressable ) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 p0 80h name p0.7 p0.6 p0.5 p0.4 p0.3 p0.2 p0.1 p0.0 p0 register could be bit-addressable. and p0.7~p0.0 coulde be set/cleared by cpu. p0m1 register (non bit addressable ) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 p0m1 93h name p0m1.7 p0m1.6 p0m1.5 p0m1.4 p0m1.3 p0m1.2 p0m1.1 p0m1.0 p0m0 register (non bit addressable ) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 p0m0 94h name p0m0.7 p0m0.6 p0m0.5 p0m0.4 p0m0.3 p0m0.2 p0m0.1 p0m0.0 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 85 www.stcmcu.com 4.2 p4/p5 of stc12c5a60s2 series the processes accessing p4 and p5 are same with common p1, p2 and p3 which all are bit addressable. the address of p4 is c0h and p5 is c8h. the address of p4 port is c0h. every bit in p4 all can be bit-addressable, bit address of p4 are shown below bit p4.7 p4.6 p4.5 p4.4 p4.3 p4.2 p4.1 p4.0 bit address c7h c6h c5h c4h c3h c2h c1h c0h the address of p5 port is c8h. every bit in p5 all can be bit-addressable, bit address of p5 are shown below bit - - - - p5.3 p5.2 p5.1 p5.0 bit address cbh cah c9h c8h p4.4, p4.5, p4.6 and p4.7 are located at the pins - psen, ale, ea and rst of conventional 80c51. pay attention that additional control bits on p4sw register are used to enable the i/o port functions of these pins. prior to use them as i/o port, the users must set the corresponding bit to enable it. register p4sw is used to set the secondary function of na/p4.4, ale/p4.5 and ex_lvd/p4.6 mnemonic add name 7 6 5 4 3 2 1 0 reset value p4sw bbh port-4 switch lvd_p4.6 ale_p4.5 na_p4.4 x000,xxxx na/p4.4: 0, p4sw.4=0 when mcu is reset. na/p4.4 is weak pull-up and no any function. 1, when p4sw.4 is set to 1, na/p4.4 is as an i/o port (p4.4) ale/p4.5: 0, p4sw.5=0 when mcu is reset. ale/p4.5 is as ale signal which is used to access external data memory . 1, when p4sw.5 is set to 1, ale/p4.4 is used as an i/o port (p4.5) lvd/p4.6: 0, p4sw.6=0 when mcu is reset. ex_lvd/p4.6 is as external low-voltage detection function 1, when p4sw.6 is set to 1, ex_lvd/p4.6 is used as an i/o port (p4.6) in stc-isp writter/programmer, users can select what rst/p4.7 is used as. the pin rst/p4.7 is as reset function acquiescently, see the following figure. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 86 www.stcmcu.com register auxr1 is used to select whether pca/pwm/spi/uart2 function is on p1 port or p4 port mnemonic add name 7 6 5 4 3 2 1 0 reset value auxr1 a2h auxiliary register 1 - pca_p4 spi_p4 s2_p4 gf2 adrj - dps x000,00x0 pca _p4 0 : default. the pca function is on p1[4:2] 1 : the pca function on p1[4:2] is switched to p4[3:1]. eci is switched from p1.2 to p4.1 pca0/pwm0 is switched from p1.3 to p4.2 pca1/pwm1 is switched from p1.4 to p4.3 spi_p4 0 : default. the spi function is on p1[7:4] 1 : the spi function on p1[7:4] is switched to p4[3:0]. sclk is switched from p1.7 to p4.3 mosi is switched from p1.6 to p4.2 miso is switched from p1.5 to p4.1 ss is switched from p1.4 to p4.0 s2_p4 0 : default. the uart2(s2) function is on p1[3:2] 1 : the uart2(s2) function on p1[3:2] is switched to p4[3:2]. txd2 is switched from p1.3 to p4.3 rxd2 is switched from p1.2 to p4.2 gf2 : general flag. it can be used by software. adrj 0 : the 10-bit conversion result of adc is arranged as {adc_res[7:0], adc_resl[1:0]}. 1 : the 10-bit conversion result is right-justified, {adc_res[1:0], adc_resl[7:0]}. dps 0 : default. dptr0 is selected as data pointer. 1 : the secondary dptr is switched to use. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 87 www.stcmcu.com 4.3.1 quasi-bidirectional i/o port pins in quasi-bidirectional output mode function similar to the standard 8051 port pins. a quasi-bidirectional port can be used as an input and output without the need to reconfigure the port. this is possible because when the port outputs a logic high, it is weakly driven, allowing an external device to pull the pin low. when the pin outputs low, it is driven strongly and able to sink a large current. there are three pull-up transistors in the quasi- bidirectional output that serve different purposes. one of these pull-ups, called the very weak pull-up, is turned on whenever the port register for the pin contains a logic 1. this very weak pull-up sources a very small current that will pull the pin high if it is left floating. a second pull-up, called the weak pull-up, is turned on when the port register for the pin contains a logic 1 and the pin itself is also at a logic 1 level. this pull-up provides the primary source current for a quasi- bidirectional pin that is outputting a 1. if this pin is pulled low by the external device, this weak pull-up turns off, and only the very weak pull-up remains on. in order to pull the pin low under these conditions, the external device has to sink enough current to over-power the weak pull-up and pull the port pin below its input threshold voltage. the third pull-up is referred to as the strong pull-up. this pull-up is used to speed up low-to-high transitions on a quasi-bidirectional port pin when the port register changes from a logic 0 to a logic 1. when this occurs, the strong pull-up turns on for two cpu clocks, quickly pulling the port pin high. vcc 2 clock delay vcc vcc port pin weak very weak strong port latch data input data quasi-bidirectional output 4.3 i/o ports modes stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 88 www.stcmcu.com 4.3.4 open-drain output the open-drain output configuration turns off all pull-ups and only drives the pull-down transistor of the port pin when the port register contains a logic 0. to use this configuration in application, a port pin must have an external pull-up, typically tied to vcc. the input path of the port pin in this configuration is the same as quasi-bidirection mode. port pin port latch data input data open-drain output 4.3.3 input-only (high-impedance)mode the input-only configuration is a schmitt-triggered input without any pull-up resistors on the pin. port pin input data input-only mode 4.3.2 push-pull output the push-pull output configuration has the same pull-down structure as both the open-drain and the quasi- bidirectional output modes, but provides a continuous strong pull-up when the port register conatins a logic 1. the push-pull mode may be used when more source current is needed from a port output. in addition, input path of the port pin in this configuration is also the same as quasi-bidirectional mode. vcc port pin port latch data input data push-pull output stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 89 www.stcmcu.com 4.4 i/o port application notes traditional 8051 access i/o (signal transition or read status) timing is 12 clocks, stc12c5a60s2 series mcu is 4 clocks. when you need to read an external signal, if internal output a rising edge signal, for the traditional 8051, this process is 12 clocks, you can read at once, but for stc12c5a60s2 series mcu, this process is 4 clocks, when internal instructions is complete but external signal is not ready, so you must delay 1~2 nop operation. when mcu is connected to a spi or i2c or other open-drain peripherals circuit, you need add a 10k pull-up resistor. some io port connected to a pnp transistor, but no pul-up resistor. the correct access method is io port pull-up resistor and transistor base resistor should be consistent, or io port is set to a strongly push-pull output mode. using io port drive led directly or matrix key scan, needs add a 470ohm to 1kohm resistor to limit current. 4.5 typical transistor control circuit if i/o is configed as weak pull-up, you should add a external pull-up resistor r1(3.3k~10k ohm). if no pull-up resistor r1 , proposal to add a 15k ohm series resistor r2 at least or config i/o as push-pull mode. common i/o port r1 10k(3.3k~10k) r3 r2 15k(3.3k~15k) 4.6 typical diode control circuit i/o 1k for weak pull-up / quasi-bidirectional i/o, use sink current drive led, current limiting resistor as greater than 1k ohm, minimum not less than 470 ohm. i/o 1k for push-pull / strong pull-up i/o, use drive current drive led. vcc vcc vcc stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 90 www.stcmcu.com 4.7 3v/5v hybrid system when stc12c5a60s2 series 5v mcu connect to 3.3v peripherals. to prevent the 3.3v device can not afford to 5v voltage, the 5v mcu corresponding i/o should first add a 330 ohm current limiting resistor to 3.3 device i/o ports. and in intialization of procedures the 5v mcu corresponding i/o is set to open drain mode, disconnect the internal pull-up resistor, the corresponding 3.3v device i/o port add 10k ohm external pull-up resistor to the 3.3v device vcc, so high level to 3.3v and low to 0v, which can proper functioning mcu common i/o external input signal when stc12le5a60s2 series 3v mcu connect to 5v peripherals. to prevent the 3v mcu can not afford to 5v voltage, if the corresponding i/o port as input port, the port may be in an isolation diode in series, isolated high- voltage part. when the external signal is higher than mcu operating voltage, the diode cut-off, i/o have been pulled high by the internal pull-up resistor; when the external signal is low, the diode conduction, i/o port voltage is limited to 0.7v, its low signal to mcu. 5v mcu i/o port 10k 330 3.3v device i/o port 3.3v when stc12le5a60s2 series 3v mcu connect to 5v peripherals. to prevent the 3v mcu can not afford to 5v voltage, if the corresponding i/o port as output port, the port may be connect a npn transistor to isolate high- voltage part. the circuit is shown as below. common i/o port 10k 2k 5v 1 0 5v device i/o port 1 0 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 91 www.stcmcu.com 4.8 how to make i/o port low after mcu reset traditional 8051 mcu power-on reset, the general io port are weak pull-high output, while many practical applications require io port remain low level after power-on reset, otherwise the system malfunction would be generated. for stc12c5a60s2 series mcu, io port can add a pull-down resistor (1k/2k/3k), so that when power-on reset, although a weak internal pull-up to make mcu output high, but because of the limited capacity of the internal pull-up, it can not pull-high the pad, so this io port is low level after power-on reset. if the i/o port need to drive high, you can set the io model as the push-pull output mode, while the push-pull mode the drive current can be up to 20ma, so it can drive this i/o high. i/o 1k/2k/3k more then 470ohm 4.9 i/o status while pwm outputing when i/o is used as pwm port, its status as bellow: before pwm output while pwm outputing quasi-bidirectional push-pull (strong pull-high need 1k~10k limiting resistor) push-pull push-pull (strong pull-high need 1k~10k limiting resistor) input ony (floating) pwm invalid open-drain open-drain common i/o port to load current limiting resistor between 10k and 1k stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 92 www.stcmcu.com 4.10 i/o drive led application circuit 31 30 29 28 27 26 25 24 23 22 21 40 39 38 37 36 35 34 33 32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 p0.3/ad3 clkout2/adc0/p1.0 adc1/p1.1 rxd2/eci/adc2/p1.2 txd2/ccp0/adc3/p1.3 ss/ccp1/adc4/p1.4 mosi/adc5/p1.5 miso/adc6/p1.6 sclk/adc7/p1.7 rst/p4.7 int/rxd/p3.0 txd/p3.1 clkout0/int/t0/p3.4 clkout1/int/t1/p3.5 xtal2 xtal1 gnd vcc p0.0/ad0 p0.1/ad1 p0.2/ad2 p0.4/ad4 p0.5/ad5 p0.6/ad6 p0.7/ad7 rst2/p4.6/ex_lvd ale/p4.5 na/p4.4 p2.7/ad15 p2.6/ad14 p2.5/ad13 p2.4/ad12 p2.3/ad11 p2.2/ad10 p2.1/ad9 p2.0/ad8 int0/p3.2 int1/p3.3 wr/p3.6 rd/p3.7 r1 a r2 b r3 c r4 d r5 e r6 f r7 g r8 dp com1 com2 com3 com4 i/o i/o i/o i/o r1 471 r2 471 r3 471 r4 471 i/o i/o i/o i/o i/o i/o i/o i/o 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 vcc p2.1 p1.7/adc7 p1.4/adc4 p1.3/adc3 p1.2/adc2/ex_lvd p1.1/adc1 p1.6/adc6 p1.5/adc5 p2.0 p1.0/adc0 p3.7/ccp0 p2.7 p2.6 rst txd/p3.1 xtal2 xtal1 gnd rxd/p3.0 int1/p3.3 clkout0/eci/t0/p3.4 clkout1/ccp1/t1/p3.5 int0/p3.2 p2.2 p2.3 p2.4 p2.5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 sop-32 p0.0 p0.1 p0.3 p0.2 led4 a r5 i/o b r6 i/o c r7 i/o d r8 i/o e r9 i/o f r10 i/o g r11 i/o dp r12 i/o i/o led3 i/o led2 i/o led1 i/o a b c d e f g dp com1 led4 r4 4k7 led3 r3 4k7 led2 r2 4k7 led1 r1 4k7 vcc com2 com3 com4 470ohm*8 i/o dynamic scan driver 4 groups of digital tube cathode circuit i/o dynamic scan driver 4 groups of digital tube anode circuit 1kohm*8 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 93 www.stcmcu.com seg1 seg2 seg3 seg4 seg5 seg6 seg7 seg8 com1 com2 com3 com4 seg1 seg2 seg3 seg4 seg5 seg6 seg7 seg8 com1 com2 com3 com4 seg1 i/o seg2 i/o seg3 i/o seg4 i/o seg5 i/o seg6 i/o seg7 i/o seg8 i/o com1 i/o i/o i/o i/o lcd4x8 com2 com3 com4 com1 r1 100k r2 100k r3 100k r4 100k r5 100k r6 100k r7 100k r8 100k vcc com2 com3 com4 how to light on the lcd pixels: when the pixels corresponding com-side and seg-side voltage difference is greater than 1/2vcc, this pixel is lit, otherwise off contrl seg-side (segment) : i/o direct drive segment lines, control segment output high-level (vcc) or low-level (0v). contrl com-side (common) : i/o port and two 100k dividing resistors jointly controlled common line, when the io output "0", the common-line is low level (0v), when the io push-pull output "1", the common line is high level (vcc), when io as high-impedance input, the common line is 1/2vcc. seg1 seg2 seg3 seg4 seg5 seg6 seg7 seg8 com1 com2 com3 com4 seg1 seg2 seg3 seg4 seg5 seg6 seg7 seg8 com1 com2 com3 com4 seg1 i/o seg2 i/o seg3 i/o seg4 i/o seg5 i/o seg6 i/o seg7 i/o seg8 i/o com1 i/o i/o i/o i/o lcd4x8 com2 com3 com4 com1 r1 100k r2 100k r3 100k r4 100k r5 100k r6 100k r7 100k r8 100k vcc com2 com3 com4 before mcu enter power_down mode, the i/o output high level, then common side will have no leakage current i/o control 4.11 i/o immediately drive lcd application circuit 1/2 bias 1/2 bias stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 94 www.stcmcu.com r6 8.2 k sw6 r5 5.4 k sw5 r4 3.3 k sw4 r3 1.8 k sw3 r2 520 sw2 sw1 r1 10k +5v adcx 47pf 0 0`0.5 0.5`1 1`1.5 1.5`2.0 2.0`2.5 r1 520 sw2 sw1 r0 10k +5v adcx 47pf 0 r2 1.2k sw3 r3 1.6k sw4 r4 1.8k sw5 r5 3k sw6 r6 4k sw7 r7 6.5 sw8 r8 10k sw9 r9 30k sw10 r10 100k sw11 4.12 using a/d conversion to scan key application circuit this circuit can achieve a signle key or combin key scan, resistance need to configure the actual needs this circuit use 10 keys spaced partial pressure, for each key, range of allowed error is +/-0.25v, it can effectively avoid failure of key detection because of resistance or temperature drift. if the requested key detection more stable and reliable, can reduce the number of buttons, to relax the voltage range of each key 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 vcc ale/p4.5 na/p4.4 p4.7/rst txd/p3.1 xtal2 xtal1 gnd wr/p3.6 rd/p3.7 int/rxd/p3.0 clkout0/int/t0/p3.4 clkout1/int/t1/p3.5 int1/p3.3 int0/p3.2 p0.0 p0.1 p0.2 p0.3 p0.4 p0.5 p0.6 p0.7 ex_lvd/p4.6/rst2 p2.7/a15 p2.6/a14 p2.5/a13 p2.4/a12 p2.3/a11 p2.2/a10 p2.1/a9 p2.0/a8 clkout2/adc0/p1.0 adc1/p1.1 rxd2/eci/adc2/p1.2 txd2/cpp0/adc3/p1.3 ss/cpp1/adc4/p1.4 mosi/adc5/p1.5 miso/adc6/p1.6 sclk/adc7/p1.7 pdip-40 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 95 www.stcmcu.com chapter 5. instruction system register-specific instruction some instructions are specific to a certain register. for example, some instructions always operate on the accumulator or data pointer,etc. no address byte is needed for such instructions. the opcode itself does it. index addressing only program memory can be accessed with indexed addressing and it can only be read. this addressing mode is intended for reading look-up tables in program memory. a 16-bit base register(either dptr or pc) points to the base of the table, and the accumulator is set up with the table entry number. another type of indexed addressing is used in the conditional jump instruction. in conditional jump, the destination address is computed as the sum of the base pointer and the accumulator. 5.1 addressing modes addressing modes are an integral part of each computer's instruction set. they allow specifyng the source or destination of data in different ways, depending on the programming situation. there are five modes available: immediate direct indirect register indexed immediate constant(imm) the value of a constant can follow the opcode in the program memory. for example, mov a, #70h loads the accumulator with the hex digits 70. the same number could be specified in decimal number as 112. direct addressing(dir) in direct addressing the operand is specified by an 8-bit address field in the instruction. only 128 lowest bytes of internal data ram and sfrs can be direct addressed. indirect addressing(ind) in indirect addressing the instruction specified a register which contains the address of the operand. both internal and external ram can be indirectly addressed. the address register for 8-bit addresses can be r0 or r1 of the selected bank, or the stack pointer. the address register for 16-bit addresses can only be the 16-bit data pointer register C dptr. register instruction(reg) the register banks, containing registers r0 through r7, can be accessed by certain instructions which carry a 3-bit register specification within the opcode of the instruction. instructions that access the registers this way are code efficient because this mode eliminates the need of an extra address byte. when such instruction is executed, one of the eight registers in the selected bank is accessed. ? ? ? ? ? stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 96 www.stcmcu.com 5.2 instruction set summary mnemonic description byte execution clocks of 12t mcu execution clocks of stc 1t mcu efficiency improved arithmetic operations add a, rn add register to accumulator 1 12 2 6x add a, direct add ditect byte to accumulator 2 12 3 4x add a, @ri add indirect ram to accumulator 1 12 3 4x add a, #data add immediate data to accumulator 2 12 2 6x addc a, rn add register to accumulator with carry 1 12 2 6x addc a, direct add direct byte to accumulator with carry 2 12 3 4x addc a, @ri add indirect ram to accumulator with carry 1 12 3 4x addc a, #data add immediate data to acc with carry 2 12 2 6x subb a, rn subtract register from acc wih borrow 1 12 2 6x subb a, direct subtract direct byte from acc with borrow 2 12 3 4x subb a, @ri subtract indirect ram from acc with borrow 1 12 3 4x subb a, #data substract immediate data from acc with borrow 2 12 2 6x inc a increment accumulator 1 12 2 6x inc rn increment register 1 12 3 4x inc direct increment direct byte 2 12 4 3x inc @ri increment direct ram 1 12 4 3x dec a decrement accumulator 1 12 2 6x dec rn decrement register 1 12 3 4x dec direct decrement direct byte 2 12 4 3x dec @ri decrement indirect ram 1 12 4 3x inc dptr increment data pointer 1 24 1 24x mul ab multiply a & b 1 48 4 12x div ab divde a by b 1 48 5 9.6x da a decimal adjust accumulator 1 12 4 3x the stc mcu instructions are fully compatible with the standard 8051's,which are divided among five functional groups: arithmetic logical data transfer boolean variable program branching the following tables provides a quick reference chart showing all the 8051 and stc 1t mcu instructions. once you are familiar with the instruction set, this chart should prove a handy and quick source of reference. ? ? ? ? ? execution clocks of conventional 12t 8051 execution clocks of stc12c5a60s2 series stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 97 www.stcmcu.com mnemonic description byte execution clocks of 12t mcu execution clocks of stc 1t mcu efficiency improved logical operations anl a, rn and register to accumulator 1 12 2 6x anl a, direct and direct btye to accumulator 2 12 3 4x anl a, @ri and indirect ram to accumulator 1 12 3 4x anl a, #data and immediate data to accumulator 2 12 2 6x anl direct, a and accumulator to direct byte 2 12 4 3x anl direct, #data and immediate data to direct byte 3 24 4 6x orl a, rn or register to accumulator 1 12 2 6x orl a,direct or direct byte to accumulator 2 12 3 4x orl a,@ri or indirect ram to accumulator 1 12 3 4x orl a, #data or immediate data to accumulator 2 12 2 6x orl direct, a or accumulator to direct byte 2 12 4 3x orl direct,#data or immediate data to direct byte 3 24 4 6x xrl a, rn exclusive-or register to accumulator 1 12 2 6x xrl a, direct exclusive-or direct byte to accumulator 2 12 3 4x xrl a, @ri exclusive-or indirect ram to accumulator 1 12 3 4x xrl a, #data exclusive-or immediate data to accumulator 2 12 2 6x xrl direct, a exclusive-or accumulator to direct byte 2 12 4 3x xrl direct,#data exclusive-or immediate data to direct byte 3 24 4 6x clr a clear accumulator 1 12 1 12x cpl a complement accumulator 1 12 2 6x rl a rotate accumulator left 1 12 1 12x rlc a rotate accumulator left through the carry 1 12 1 12x rr a rotate accumulator right 1 12 1 12x rrc a rotate accumulator right through the carry 1 12 1 12x swap a swap nibbles within the accumulator 1 12 1 12x stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 98 www.stcmcu.com mnemonic description byte execution clocks of 12t mcu execution clocks of stc 1t mcu efficiency improved data transfer mov a, rn move register to accumulator 1 12 1 12x mov a, direct move direct byte to accumulator 2 12 2 6x mov a,@ri move indirect ram to 1 12 2 6x mov a, #data move immediate data to accumulator 2 12 2 6x mov rn, a move accumulator to register 1 12 2 6x mov rn, direct move direct byte to register 2 24 4 6x mov rn, #data move immediate data to register 2 12 2 6x mov direct, a move accumulator to direct byte 2 12 3 4x mov direct, rn move register to direct byte 2 24 3 8x mov direct,direct move direct byte to direct 3 24 4 6x mov direct, @ri move indirect ram to direct byte 2 24 4 6x mov direct, #data move immediate data to direct byte 3 24 3 8x mov @ri, a move accumulator to indirect ram 1 12 3 4x mov @ri, direct move direct byte to indirect ram 2 24 4 6x mov @ri, #data move immediate data to indirect ram 2 12 3 4x mov dptr, #data16 move immdiate data to indirect ram 2 24 3 8x movc a, @a+dptr move code byte relative to dptr to acc 1 24 4 6x movc a, @a+pc move code byte relative to pc to acc 1 24 4 6x movx a, @ri move external ram(8-bit addr) to acc 1 24 3 8x movx @ri, a move acc to external ram(8-bit addr) 1 24 4 6x movx a, @dptr move external ram(16-bit addr) to acc 1 24 3 8x movx @dptr, a move acc to external ram (16-bit addr) 1 24 3 8x push direct push direct byte onto stack 2 24 4 6x pop direct pop direct byte from stack 2 24 3 8x xch a, rn exchange register with accumulator 1 12 3 4x xch a, direct exchange direct byte with accumulator 2 12 4 3x xch a, @ri exchange indirect ram with accumulator 1 12 4 3x xchd a, @ri exchange low-order digit indirect ram with acc 1 12 4 3x stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 99 www.stcmcu.com mnemonic description byte execution clocks of 12t mcu execution clocks of stc 1t mcu efficiency improved boolean variable manipulation clr c clear carry 1 12 1 12x clr bit clear direct bit 2 12 4 3x setb c set carry 1 12 1 12x setb bit set direct bit 2 12 4 3x cpl c complement carry 1 12 1 12x cpl bit complement direct bit 2 12 4 3x anl c, bit and direct bit to carry 2 24 3 8x anl c, /bit and complement of direct bit to carry 2 24 3 8x orl c, bit or direct bit to carry 2 24 3 8x orl c, /bit or complement of direct bit to carry 2 24 3 8x mov c, bit move direct bit to carry 2 12 3 4x mov bit, c move carry to direct bit 2 24 4 6x jc rel jump if carry is set 2 24 3 8x jnc rel jump if carry not set 2 24 3 8x jb bit, rel jump if direct bit is set 3 24 4 6x jnb bit,rel jump if direct bit is not set 3 24 4 6x jbc bit, rel jump if direct bit is set & clear bit 3 24 5 4.8x program branching acall addr11 absolute subroutine call 2 24 6 4x lcall addr16 long subroutine call 3 24 6 4x ret return from subroutine 1 24 4 6x reti return from interrupt 1 24 4 6x ajmp addr11 absolute jump 2 24 3 8x ljmp addr16 long jump 3 24 4 6x sjmp rel short jump (relative addr) 2 24 3 8x jmp @a+dptr jump indirect relative to the dptr 1 24 3 8x jz rel jump if accumulator is zero 2 24 3 8x jnz rel jump if accumulator is not zero 2 24 3 8x cjne a,direct,rel compare direct byte to acc and jump if not equal 3 24 5 4.8x cjne a,#data,rel compare immediate to acc and jump if not equal 3 24 4 6x cjne rn,#data,rel compare immediate to register and jump if not equal 3 24 4 6x cjne @ri,#data,rel compare immediate to indirect and jump if not equal 3 24 5 4.8x djnz rn, rel decrement register and jump if not zero 2 24 4 6x djnz direct, rel decrement direct byte and jump if not zero 3 24 5 4.8x nop no operation 1 12 1 12x stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 100 www.stcmcu.com instruction execution speed boost summary: 24 times faster execution speed 1 12 times faster execution speed 12 9.6 times faster execution speed 1 8 times faster execution speed 20 6 times faster execution speed 39 4.8 times faster execution speed 4 4 times faster execution speed 20 3 times faster execution speed 14 24 times faster execution speed 1 based on the analysis of frequency of use order statistics, stc 1t series mcu instruction execution speed is faster than the traditional 8051 mcu 8 ~ 12 times in the same working environment. instruction execution clock count: 1 clock instruction 12 2 clock instruction 20 3 clock instruction 38 4 clock instruction 34 5 clock instruction 5 6 clock instruction 2 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 101 www.stcmcu.com 5.3 instruction definitions acall addr 11 function: a bsolute call description: acall unconditionally calls a subroutine located at the indicated address.the instruction increments the pc twice to obtain the address of the following instruction, then pushes the 16-bit result onto the stack (low-order byte first) and increments the stack pointer twice. the destination address is obtained by suceesively concatenating the five high-order bits of the incremented pc opcode bits 7-5,and the second byte of the instruction. the subroutine called must therefore start within the same 2k block of the program memory as the first byte of the instruction following acall. no flags are affected. example: initially sp equals 07h. the label subrtn is at program memory location 0345h. after executingthe instruction, acall subrtn at location 0123h, sp will contain 09h, internal ram locations 08h and 09h will contain 25h and 01h, respectively, and the pc will contain 0345h. bytes: 2 cycles: 2 encoding: a10 a9 a8 1 0 0 1 0 a7 a6 a5 a4 a3 a2 a1 a0 operation: acall (pc)? (pc)? 2 ? (pc) ? 2 (pc)+ 2 (sp)?(sp) ? 1 ?(sp) ? 1 (sp) + 1 ((sp)) ? (pc ? (pc (pc 7-0 ) (sp)?(sp) ? 1 ?(sp) ? 1 (sp) + 1 ((sp))?(pc ?(pc (pc 15-8 ) (pc 10-0 )? p??e ???ress ? p ??e ???ress page address add a, function: add description: add adds the byte variable indicated to the accumulator, leaving the result in the accumulator. the carry and auxiliary-carry flags are set, respectively, if there is a carry- out from bit 7 or bit 3, and cleared otherwise. when adding unsigned integers, the carry flag indicates an overflow occured. ov is set if there is a carry-out of bit 6 but not out of bit 7, or a carry-out of bit 7 but not bit 6; otherwise ov is cleared. when adding signed integers, ov indicates a negative number produced as the sum of two positive operands, or a positive sum from two negative operands. four source operand addressing modes are allowed: register,direct register-indirect, or immediate. example: the accumulator holds 0c3h(11000011b) and register 0 holds 0aah (10101010b). the instruction, add a,r0 will leave 6dh (01101101b) in the accumulator with the ac flag cleared and both the carry flag and ov set to 1. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 102 www.stcmcu.com add a,rn bytes: 1 cycles: 1 encoding: 0 0 1 0 1 r r r operation: add (a)?(a) ? (?n) ?(a) ? (?n) (a) + (rn) add a,direct bytes: 2 cycles: 1 encoding: 0 0 1 0 0 1 0 1 direct address operation: add (a)?(a) ? (?ire?t) ?(a) ? (?ire?t) (a) + (direct) add a,@ri bytes: 1 cycles: 1 encoding: 0 0 1 0 0 1 1 i operation: add (a)?(a) ? ((?i)) ?(a) ? ((?i)) (a) + ((ri)) add a,#data bytes: 2 cycles: 1 encoding: 0 0 1 0 0 1 0 0 immediate data operation: add (a)?(a) ? ??? t? ?(a) ? ??? t? (a) + #data addc a, function: add with carry description: addc simultaneously adds the byte variable indicated, the carry flag and the accumulator, leaving the result in the accumulator. the carry and auxiliary-carry flags are set, respectively, if there is a carry-out from bit 7 or bit 3, and cleared otherwise. when adding unsigned integers, the carry flag indicates an overflow occured. ov is set if there is a carry-out of bit 6 but not out of bit 7, or a carry-out of bit 7 but not out of bit 6; otherwise ov is cleared. when adding signed integers, ov indicates a negative number produced as the sum of two positive operands or a positive sum from two negative operands. four source operand addressing modes are allowed: register, direct, register-indirect, or immediate. example: the accumulator holds 0c3h(11000011b) and register 0 holds 0aah (10101010b) with the carry. the instruction, addc a,r0 will leave 6eh (01101101b) in the accumulator with the ac flag cleared and both the carry flag and ov set to 1. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 103 www.stcmcu.com addc a,rn bytes: 1 cycles: 1 encoding: 0 0 1 1 1 r r r operation: addc (a)?(a) ? (c) ? (?n) ?(a) ? (c) ? (?n) (a) + (c) + (rn) addc a,direct bytes: 2 cycles: 1 encoding: 0 0 1 1 0 1 0 1 direct address operation: addc (a)?(a) ? (c) ? (?ire?t) ?(a) ? (c) ? (?ire?t) (a) + (c) + (direct) addc a,@ri bytes: 1 cycles: 1 encoding: 0 0 1 1 0 1 1 i operation: addc (a)?(a) ? (c) ? ((?i)) ?(a) ? (c) ? ((?i)) (a) + (c) + ((ri)) addc a,#data bytes: 2 cycles: 1 encoding: 0 0 1 1 0 1 0 0 immediate data operation: addc (a)?(a) ? (c) ? ??? t? ?(a) ? (c) ? ??? t? (a) + (c) + #data ajmp addr 11 function: a bsolute jump description: ajmp transfers program execution to the indicated address, which is formed at run-time by concatenating the high-order five bits of the pc (after incrementing the pc twice), opcode bits 7-5, and the second byte of the instruction. the destination must therefore be within the same 2k block of program memory as the first byte of the instruction following ajmp. example: the label jmpadr is at program memory location 0123h. the instruction, ajmp jmpadr is at location 0345h and will load the pc with 0123h. bytes: 2 cycles: 2 encoding: a10 a9 a8 0 0 0 0 1 a7 a6 a5 a4 a3 a2 a1 a0 operation: ajmp (pc)? (pc)? 2 ? (pc) ? 2 (pc)+ 2 (pc 10-0 )? p??e ???ress ? p ??e ???ress page address stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 104 www.stcmcu.com anl , function: logical-and for byte variables description: anl performs the bitwise logical-and operation between the variables indicated and stores the results in the destination variable. no flags are affected. the two operands allow six addressing mode combinations. when the destination is the accumulator, the source can use register, direct, register-indirect, or immediate addressing; when the destination is a direct address, the source can be the accumulator or immediate data. note: when this instruction is used to modify an output port, the value used as the original port data will be read from the output data latch not the input pins. example: if the accumulator holds 0c3h(11000011b) and register 0 holds 55h (01010101b) then the instruction, anl a,r0 will leave 41h (01000001b) in the accumulator. when the destination is a directly addressed byte, this instruction will clear combinations of bits in any ram location or hardware register. the mask byte determining the pattern of bits to be cleared would either be a constant contained in the instruction or a value computed in t he accumulator at run-time. the instruction, anl pl, #01110011b will clear bits 7, 3, and 2 of output port 1. anl a,rn bytes: 1 cycles: 1 encoding: 0 1 0 1 1 r r r operation: anl (a)?(a) ?(a) (a) (rn) anl a,direct bytes: 2 cycles: 1 encoding: 0 1 0 1 0 1 0 1 direct address operation: anl (a)?(a) ?(a) (a) (direct) anl a,@ri bytes: 1 cycles: 1 encoding: 0 1 0 1 0 1 1 i operation: anl (a)?(a) ?(a) (a) ((ri)) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 105 www.stcmcu.com anl a,#data bytes: 2 cycles: 1 encoding: 0 1 0 1 0 1 0 0 immediate data operation: anl (a)?(a) ?(a) (a) #data anl direct,a bytes: 2 cycles: 1 encoding: 0 1 0 1 0 0 1 0 direct address operation: anl (?ire?t)?(?ire?t) ?(?ire?t) (direct) (a) anl direct,#data bytes: 3 cycles: 2 encoding: 0 1 0 1 0 0 1 1 direct address immediate data operation: anl (?ire?t)?(?ire?t) ?(?ire?t) (direct) #data anl c , function: logical-and for bit variables description: if the boolean value of the source bit is a logical 0 then clear the carry flag; otherwise leave the carry flag in its current state. a slash ( / ) preceding the operand in the assembly language indicates that the logical complement of the addressed bit is used as the source value, but the source bit itself is not affceted. no other flsgs are affected. only direct addressing is allowed for the source operand. example: set the carry flag if, and only if, p1.0 = 1, acc. 7 = 1, and ov = 0: mov c, p1.0 ;load carry with input pin state anl c, acc.7 ;and carry with accum. bit.7 anl c, /ov ;and with inverse of overflow flag anl c,bit bytes: 2 cycles: 2 encoding: 1 0 0 0 0 0 1 0 bit address operation: anl (c) ? (c) ? (c) (c) (bit) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 106 www.stcmcu.com anl c, /bit bytes: 2 cycles: 2 encoding: 1 0 1 1 0 0 0 0 bit address operation: add (c)?(c) ?(c) (c) (bit) cjne , , rel function: compare and jump if not equal description: cjne compares the magnitudes of the first two operands, and branches if their values are not equal. the branch destination is computed by adding the signed relative-displacement in the last instruction byte to the pc, after incrementing the pc to the start of the next instruction. the carry flag is set if the unsigned integer value of is less than the unsigned in teger value of ; otherwise, the carry is cleared. neither operand is affected. the first two operands allow four addressing mode combinations: the accumulator may be compared with any directly addressed byte or immediate data, and any indirect ram location or working register can be compared with an immediate constant. example: the accumulator contains 34h. register 7 contains 56h. the first instruction in the sequenc e cjne r7,#60h, not-eq ; . . . . . . . . . ; r7 = 60h. not_eq: jc req_low ; if r7 < 60h. ; . . . . . . . . ; r7 > 60h. sets the carry flag and branches to the instruction at label not-eq. by testing the carry flag, t his instruction determines whether r7 is greater or less than 60h. if the data being presented to port 1 is also 34h, then the instruction, wait: cjne a,p1,wait clears the carry flag and continues with the next instruction in sequence, since the accumulator does equal the data read from p1. (if some other value was being input on pl, the program will loop at this point until the p1 data changes to 34h.) cjne a,direct,rel bytes: 3 cycles: 2 encoding: 1 0 1 1 0 1 0 1 direct address rel. address operation: (pc) ? (pc) ? 3 ? (pc) ? 3 (pc) + 3 if (a) < > (direct) then (pc) ? (pc) ? ? (pc) ? (pc) + relative offset if (a) < (direct) then (c) ? 1 ? 1 1 else (c) ? 0 ? 0 0 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 107 www.stcmcu.com cjne a,#data,rel bytes: 3 cycles: 2 encoding: 1 0 1 1 0 1 0 1 immediata data rel. address operation: (pc) ? (pc) ? 3 ? (pc) ? 3 (pc) + 3 if (a) < > (data) then (pc) ? (pc) ? ? (pc) ? (pc) + relative offset if (a) < (data) then (c) ? 1 ? 1 1 else (c) ? 0 ? 0 0 cjne rn,#data,rel bytes: 3 cycles: 2 encoding: 1 0 1 1 1 r r r immediata data rel. address operation: (pc) ? (pc) ? 3 ? (pc) ? 3 (pc) + 3 if (rn) < > (data) then (pc) ? (pc) ? ? (pc) ? (pc) + relative offset if (rn) < (data) then (c) ? 1 ? 1 1 else (c) ? 0 ? 0 0 cjne @ri,#data,rel bytes: 3 cycles: 2 encoding: 1 0 1 1 0 1 1 i immediate data rel. address operation: (pc) ? (pc) ? 3 ? (pc) ? 3 (pc) + 3 if ((ri)) < > (data) then (pc) ? (pc) ? ? (pc) ? (pc) + relative offset if ((ri)) < (data) then (c) ? 1 ? 1 1 else (c) ? 0 ? 0 0 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 108 www.stcmcu.com clr a function: clear accumulator description: the aecunmlator is cleared (all bits set on zero). no flags are affected. example: the accumulator contains 5ch (01011100b). the instruction, clr a will leave the accumulator set to 00h (00000000b). bytes: 1 cycles: 1 encoding: 1 1 1 0 0 1 0 0 operation: clr (a)? 0 ? 0 0 clr bit function: clear bit description: the indicated bit is cleared (reset to zero). no other flags are affected. clr can operate on the carry flag or any directly addressable bit. example: port 1 has previously been written with 5dh (01011101b). the instruction, clr p1.2 will leave the port set to 59h (01011001b). clr c bytes: 1 cycles: 1 encoding: 1 1 0 0 0 0 1 1 operation: clr (c) ? 0 ? 0 0 clr bit bytes: 2 cycles: 1 encoding: 1 1 0 0 0 0 1 0 bit address operation: clr (bit) ? 0 ? 0 0 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 109 www.stcmcu.com cpl a function: complement accumulator description: each bit of the accumulator is logically complemented (ones complement). bits which previously contained a one are changed to a zero and vice-versa. no flags are affected. example: the accumulator contains 5ch(01011100b). the instruction, cpl a will leave the accumulator set to 0a3h (101000011b). bytes: 1 cycles: 1 encoding: 1 1 1 1 0 1 0 0 operation: cpl (a)? ? (a) cpl bit function: complement bit description: the bit variable specified is complemented. a bit which had been a one is changed to zero and vice-versa. no other flags are affected. clr can operate on the carry or any directly addressable bit. note:when this instruction is used to modify an output pin, the value used as the original data will be read from the output data latch, not the input pin. example: port 1 has previously been written with 5dh (01011101b). the instruction, clr p1.1 clr p1.2 will leave the port set to 59h (01011001b). cpl c bytes: 1 cycles: 1 encoding: 1 0 1 1 0 0 1 1 operation: cpl (c) ? ? (c) cpl bit bytes: 2 cycles: 1 encoding: 1 0 1 1 0 0 1 0 bit address operation: cpl (bit) ? ? (bit) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 110 www.stcmcu.com da a function: decimal-adjust accumulator for addition description: da a adjusts the eight-bit value in the accumulator resulting from the earlier addition of two variables (each in packed-bcd format), producing two four-bit digits.any add or addc instruction may have been used to perform the addition. if accumulator bits 3-0 are greater than nine (xxxx1010-xxxx1111), or if the ac flag is one, six is added to the accumulator producing the proper bcd digit in the low-order nibble. this internal addition would set the carry flag if a carry-out of the low-order four-bit field propagated through all high-order bits, but it would not clear the carry flag otherwise. if the carry flag is now set or if the four high-order bits now exceed nine(1010xxxx- 111xxxx), these high-order bits are incremented by six, producing the proper bcd digit in the high-order nibble. again, this would set the carry flag if there was a carry-out of the high-order bits, but wouldnt clear the carry. the carry flag thus indicates if the sum of the original two bcd variables is greater than 100, allowing multiple precision decimal addition. ov is not affected. all of this occurs during the one instruction cycle. essentially, this instruction performs the decimal conversion by adding 00h, 06h, 60h, or 66h to the accumulator, depending on initial accumulator and psw conditions. note: da a cannot simply convert a hexadecimal number in the accumulator to bcd notation, nor does da a apply to decimal subtraction. example: the accumulator holds the value 56h(01010110b) representing the packed bcd digits of the decimal number 56. register 3 contains the value 67h (01100111b) representing the packed bcd digits of the decimal number 67.the carry flag is set. the instruction sequence. addc a,r3 da a will first perform a standard twos-complement binary addition, resulting in the value 0beh (10111110) in the accumulator. the carry and auxiliary carry flags will be cleared. the decimal adjust instruction will then alter the accumulator to the value 24h (00100100b), indicating the packed bcd digits of the decimal number 24, the low-order two digits of the decimal sum of 56,67, and the carry-in. the carry flag will be set by the decimal adjust instruction, indicating that a decimal overflow occurred. the true sum 56, 67, and 1 is 124. bcd variables can be incremented or decremented by adding 01h or 99h. if the accumula- tor initially holds 30h (representing the digits of 30 decimal), then the instruction sequence, add a,#99h da a will leave the carry set and 29h in the accumulator, since 30+99=129. the low-order byte of the sum can be interpreted to mean 30 C 1 = 29. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 111 www.stcmcu.com bytes: 1 cycles: 1 encoding: 1 1 0 1 0 1 0 0 operation: da -contents of accumulator are bcd if [[(a 3-0 ) > 9] v [(ac) = 1]] then(a 3-0 ) ? (a ? (a (a 3-0 ) + 6 and if [[(a 7-4 ) > 9] v [(c) = 1]] then (a 7-4 ) ? (a ? (a (a 7-4 ) + 6 dec byte function: decrement description: the variable indicated is decremented by 1. an original value of 00h will underflow to 0ffh. no flags are affected. four operand addressing modes are allowed: accumulator, register, direct, or register-indirect. note: when this instruction is used to modify an output port, the value used as the original port data will be read from the output data latch, not the input pins. example: register 0 contains 7fh (01111111b). internal ram locations 7eh and 7fh contain 00h and 40h, respectively. the instruction sequence, dec @r0 dec r0 dec @r0 will leave register 0 set to 7eh and internal ram locations 7eh and 7fh set to 0ffh and 3fh. dec a bytes: 1 cycles: 1 encoding: 0 0 0 1 0 1 0 0 operation: dec (a)?(a) ?(a) (a) -1 dec rn bytes: 1 cycles: 1 encoding: 0 0 0 1 1 r r r operation: dec (?n)?(? n) - 1 ?(? n) - 1 (rn) - 1 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 112 www.stcmcu.com dec direct bytes: 2 cycles: 1 encoding: 0 0 0 1 0 1 0 1 direct address operation: dec (?ire?t)?(?ire?t) ?(?ire?t) (direct) -1 dec @ri bytes: 1 cycles: 1 encoding: 0 0 0 1 0 1 1 i operation: dec ((?i))?((? i)) - 1 ?((? i)) - 1 ((ri)) - 1 div ab function: divide description: div ab divides the unsigned eight-bit integer in the accumulator by the unsigned eight-bit integer in register b. the accumulator receives the integer part of the quotient; register b receives the integer remainder. the carry and ov flags will be cleared. exception: if b had originally contained 00h, the values returned in the accumulator and b-register will be undefined and the overflow flag will be set. the carry flag is cleared in any case. example: the accumulator contains 251(ofbh or 11111011b) and b contains 18(12h or 00010010b). the instruction, div ab will leave 13 in the accumulator (0dh or 00001101b) and the value 17 (11h or 00010010b) in b, since 251 = (1318) + 17. carry and ov will both be cleared. bytes: 1 cycles: 4 encoding: 1 0 0 0 0 1 0 0 operation: div (a) 15-8 (b) 7-0 ? (a) ?(?) (a)/(b) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 113 www.stcmcu.com djnz , function: decrement and jump if not zero description: djnz decrements the location indicated by 1, and branches to the address indicated by the second operand if the resulting value is not zero. an original value of 00h will underflow to 0ffh. no flags are afected. the branch destination would be computed by adding the signed relative-displacement value in the last instruction byte to the pc, after incrementing the pc to the first byte of the following instruction. the location decremented may be a register or directly addressed byte. note: when this instruction is used to modify an output port, the value used as the original port data will be read from the output data latch, not the input pins. example: internal ram locations 40h, 50h, and 60h contain the values 01h, 70h, and 15h, respectively. the instruction sequence, djnz 40h, label_1 djnz 50h, label_2 d jnz 60h, label_3 will cause a jump to the instruction at label label_2 with the values 00h, 6fh, and 15h in t he three ram locations. the first jump was not taken because the result was zero. this instruction provides a simple way of executing a program loop a given number of times, or for adding a moderate time delay (from 2 to 512 machine cycles) with a single instruction th e instruction sequence, mov r2,#8 tooole: cpl p1.7 djnz r2, toogle will toggle p1.7 eight times, causing four output pulses to appear at bit 7 of output port 1. each pulse will last three machine cycles; two for djnz and one to alter the pin. djnz rn,rel bytes: 2 cycles: 2 encoding: 1 1 0 1 1 r r r rel. address operation: djnz (pc) ? (pc) ? 2 ? (pc) ? 2 (pc) + 2 (?n) ? (?n) ? 1 ? (?n) ? 1 (rn) C 1 if (rn) > 0 or (rn) < 0 then (pc) ? (pc)? rel ? (pc) ? rel (pc)+ rel djnz direct, rel bytes: 3 cycles: 2 encoding: 1 1 0 1 0 1 0 1 direct address rel. address stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 114 www.stcmcu.com operation: djnz (pc) ? (pc) ? 2 ? (pc) ? 2 (pc) + 2 (?ire?t) ? (?ire?t) ? 1 ? ( ?ire?t) ? 1 (direct) C 1 if (direct) > 0 or (direct) < 0 then (pc) ? (pc) ? rel ? (pc) ? rel (pc) + rel inc function: increment description: inc increments the indicated variable by 1. an original value of 0ffh will overflow to 00h.no flags are affected. three addressing modes are allowed: register, direct, or register- indirect. note: when this instruction is used to modify an output port, the value used as the original port data will be read from the output data latch, not the input pins. example: register 0 contains 7eh (011111110b). internal ram locations 7eh and 7fh contain 0ffh and 40h, respectively. the instruction sequence, inc @r0 inc r0 inc @r0 will leave register 0 set to 7fh and internal ram locations 7eh and 7fh holding (respectively) 00h and 41h. inc a bytes: 1 cycles: 1 encoding: 0 0 0 0 0 1 0 0 operation: inc (a) ? (a)?1 ? (a)?1 inc rn bytes: 1 cycles: 1 encoding: 0 0 0 0 1 r r r operation: inc (?n) ? (?n)?1 ? (?n)?1 inc direct bytes: 2 cycles: 1 encoding: 0 0 0 0 0 1 0 1 direct address operation: inc (?ire?t)?(?ire?t) ?(?ire?t) (direct) + 1 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 115 www.stcmcu.com inc @ri bytes: 1 cycles: 1 encoding: 0 0 0 0 0 1 1 i operation: inc ((?i))?((?i)) ? 1 ?((?i)) ? 1 ((ri)) + 1 inc dptr function: increment data pointer description: increment the 16-bit data pointer by 1. a 16-bit increment (modulo 2 16 ) is performed; an overflow of the low-order byte of the data pointer (dpl) from 0ffh to 00h will increment the high-order-byte (dph). no flags are affected. this is the only 16-bit register which can be incremented. example: register dph and dpl contains 12h and 0feh,respectively. the instruction sequence, inc dptr inc dptr inc dptr will change dph and dpl to 13h and 01h. bytes: 1 cycles: 2 encoding: 1 0 1 0 0 0 1 1 operation: inc (dpt?) ? (dpt?)?1 ? (dpt?)?1 jb bit, rel function: jump if bit set description: if the indicated bit is a one, jump to the address indicated; otherwise proceed with the next instruction. the branch destination is computed by adding the signed relative-displacement in the third instruction byte to the pc, after incrementing the pc to the first byte of the next instruction. the bit tested is not modified. no flags are affected. example: the data present at input port 1 is 11001010b. the accumulator holds 56 (01010110b). the instruction sequence, jb p1.2, label1 jb acc.2, label2 will cause program execution to branch to the instruction at label label2. bytes: 3 cycles: 2 encoding: 0 0 1 0 0 0 0 0 bit address rel. address operation: jb (pc) ? (pc)? 3 ? (pc)? 3 (pc)+ 3 if (bit) = 1 then (pc) ? (pc) ? rel ? (pc) ? rel (pc) + rel stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 116 www.stcmcu.com jbc bit, rel function: jump if bit is set and clear bit description: if the indicated bit is one,branch to the address indicated;otherwise proceed with the next instruction. the bit wili not be cleared if it is already a zero. the branch destination is computed by adding the signed relative-displacement in the third instruction byte to the pc, a fter incrementing the pc to the first byte of the next instruction. no flags are affected. note: when this instruction is used to test an output pin, the value used as the original data will be read from the output data latch, not the input pin. example: the accumulator holds 56h (01010110b). the instruction sequence, jbc acc.3, label1 jbc acc.2, label2 will cause program execution to continue at the instruction identified by the label label2, with the accumulator modified to 52h (01010010b). bytes: 3 cycles: 2 encoding: 0 0 0 1 0 0 0 0 bit address rel. address operation: jbc (pc) ? (pc)? 3 ? (pc)? 3 (pc)+ 3 if (bit) = 1 then (bit) ? 0 ? 0 (pc) ? (pc) ? rel ? (pc) ? rel (pc) + rel jc rel function: jump if carry is set description: if the carry flag is set, branch to the address indicated; otherwise proceed with the next instruction. the branch destination is computed by adding the signed relative-displacement in the second instruction byte to the pc, after incrementing the pc twice.no flags are affected. example: the carry flag is cleared. the instruction sequence, jc label1 cpl c jc label2s will set the carry and cause program execution to continue at the instruction identified by the label label2. bytes: 2 cycles: 2 encoding: 0 1 0 0 0 0 0 0 rel. address operation: jc (pc) ? (pc)? 2 ? (pc)? 2 (pc)+ 2 if (c) = 1 then (pc) ? (pc) ? rel ? (pc) ? rel (pc) + rel stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 117 www.stcmcu.com jmp @a+dptr function: jump indirect description: add the eight-bit unsigned contents of the accumulator with the sixteen-bit data pointer, and load the resulting sum to the program counter. this will be the address for subsequent instruction fetches. sixteen-bit addition is performed (modulo 2 16 ): a carry-out from the low- order eight bits propagates through the higher-order bits. neither the accumulator nor the data pointer is altered. no flags are affected. example: an even number from 0 to 6 is in the accumulator. the following sequence of instructions will branch to one of four ajmp instructions in a jump table starting at jmp_tbl: mov dptr, #jmp_tbl jmp @a+dptr jmp-tbl: ajmp label0 ajmp label1 ajmp label2 ajmp label3 if the accumulator equals 04h when starting this sequence, execution will jump to label label2. remember that ajmp is a two-byte instruction, so the jump instructions start at every other address. bytes: 1 cycles: 2 encoding: 0 1 1 1 0 0 1 1 operation: jmp (pc) ? (a) ? (dpt?) ? (a) ? (dpt?) (a) + (dptr) jnb bit, rel function: jump if bit is not set description: if the indicated bit is a zero, branch to the indicated address; otherwise proceed with the next instruction. the branch destination is computed by adding the signed relative-displacement in the third instruction byte to the pc, after incrementing the pc to the first byte of the next instruction. the bit tested is not modified. no flags are affected. example: the data present at input port 1 is 11001010b. the accumulator holds 56h (01010110b). t he instruction sequence, jnb p1.3, label1 j nb acc.3, label2 will cause program execution to continue at the instruction at label label2 bytes: 3 cycles: 2 encoding: 0 0 1 1 0 0 0 0 bit address rel. address operation: jnb (pc) ? (pc)? 3 ? (pc)? 3 (pc)+ 3 if (bit) = 0 then (pc) ? (pc) ? rel ? (pc) ? rel (pc) + rel stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 118 www.stcmcu.com jnc rel function: jump if carry not set description: if the carry flag is a zero, branch to the address indicated; otherwise proceed with the next instruction. the branch destination is computed by adding the signed relative-displacement in the second instruction byte to the pc, after incrementing the pc twice to point to the next instruction. the carry flag is not modified example: the carry flag is set. the instruction sequence, jnc label1 cpl c jnc label2 will clear the carry and cause program execution to continue at the instruction identified by the label label2. bytes: 2 cycles: 2 encoding: 0 1 0 1 0 0 0 0 rel. address operation: jnc (pc) ? (pc)? 2 ? (pc)? 2 (pc)+ 2 if (c) = 0 then (pc) ? (pc) ? rel ? (pc) ? rel (pc) + rel jnz rel function: jump if accumulator not zero description: if any bit of the accumulator is a one, branch to the indicated address; otherwise proceed with the next instruction. the branch destination is computed by adding the signed relative- displacement in the second instruction byte to the pc, after incrementing the pc twice. the accumulator is not modified. no flags are affected. example: the accumulator originally holds 00h. the instruction sequence, jnz label1 inc a jnz laeel2 will set the accumulator to 01h and continue at label label2. bytes: 2 cycles: 2 encoding: 0 1 1 1 0 0 0 0 rel. address operation: jnz (pc) ? (pc)? 2 ? (pc)? 2 (pc)+ 2 if (a) 0 then (pc) ? (pc) ? rel ? (pc) ? rel (pc) + rel stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 119 www.stcmcu.com jz rel function: jump if accumulator zero description: if all bits of the accumulator are zero, branch to the address indicated; otherwise proceed with the next instruction. the branch destination is computed by adding the signed relative- displacement in the second instruction byte to the pc, after incrementing the pc twice. the accumulator is not modified. no flags are affected. example: the accumulator originally contains 01h. the instruction sequence, jz label1 dec a jz laeel2 will change the accumulator to 00h and cause program execution to continue at the instruction identified by the label label2. bytes: 2 cycles: 2 encoding: 0 1 1 0 0 0 0 0 rel. address operation: jz (pc) ? (pc)? 2 ? (pc)? 2 (pc)+ 2 if (a) = 0 then (pc) ? (pc) ? rel ? (pc) ? rel (pc) + rel lcall addr16 function: long call description: lcall calls a subroutine loated at the indicated address. the instruction adds three to the program counter to generate the address of the next instruction and then pushes the 16-bit result onto the stack (low byte first), incrementing the stack pointer by two. the high-order and low-order bytes of the pc are then loaded, respectively, with the second and third bytes of the lcall instruction. program execution continues with the instruction at this address. the subroutine may therefore begin anywhere in the full 64k-byte program memory address space. no flags are affected. example: initially the stack pointer equals 07h. the label subrtn is assigned to program memory l ocation 1234h. after executing the instruction, lcall subrtn at location 0123h, the stack pointer will contain 09h, internal ram locations 08h and 09h will contain 26h and 01h, and the pc will contain 1234h. bytes: 3 cycles: 2 encoding: 0 0 0 1 0 0 1 0 addr15-addr8 addr7-addr0 operation: lcall (pc) ? (pc) ? 3 ? (pc) ? 3 (pc) + 3 (sp) ? (sp) ? 1 ? (sp) ? 1 (sp) + 1 ((sp)) ? (pc ? (pc (pc 7-0 ) (sp) ? (sp) ? 1 ? (sp) ? 1 (sp) + 1 ((sp)) ? (pc ? (pc (pc 15-8 ) (pc) ? ??? r ? ???r addr 15-0 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 120 www.stcmcu.com ljmp addr16 function: long jump description: ljmp causes an unconditional branch to the indicated address, by loading the high-order and low-order bytes of the pc (respectively) with the second and third instruction bytes. the destination may therefore be anywhere in the full 64k program memory address space. no flags are affected. example: the label jmpadr is assigned to the instruction at program memory location 1234h. the instruction, ljmp jmpadr at location 0123h will load the program counter with 1234h. bytes: 3 cycles: 2 encoding: 0 0 0 0 0 0 1 0 addr15-addr8 addr7-addr0 operation: ljmp (pc) ? ??? r ? ???r addr 15-0 mov , function: move byte variable description: the byte variable indicated by the second operand is copied into the location specified by the first operand. the source byte is not affected. no other register or flag is affected. this is by far the most flexible operation. fifteen combinations of source and destination addressing modes are allowed. example: internal ram location 30h holds 40h. the value of ram location 40h is 10h. the data present at input port 1 is 11001010b (0cah). mov r0, #30h ;r0< = 30h mov a, @r0 ;a < = 40h mov r1, a ;r1 < = 40h mov b, @rl ;b < = 10h mov @rl, pl ;ram (40h) < = 0cah mov p2, p1 ;p2 #0cah leaves the value 30h in register 0,40h in both the accumulator and register 1,10h in register b, and 0cah(11001010b) both in ram location 40h and output on port 2. mov a,rn bytes: 1 cycles: 1 encoding: 1 1 1 0 1 r r r operation: mov (a) ? (?n) ? ( ?n) (rn) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 121 www.stcmcu.com *mov a,direct bytes: 2 cycles: 1 encoding: 1 1 1 0 0 1 0 1 direct address operation: mov (a)? (?ire?t) ? ( ?ire?t) (direct) *mov a, acc is not a valid instruction mov a,@ri bytes: 1 cycles: 1 encoding: 1 1 1 0 0 1 1 i operation: mov (a) ? ((?i)) ? (( ?i)) ((ri)) mov a,#data bytes: 2 cycles: 1 encoding: 0 1 1 1 0 1 0 0 immediate data operation: mov (a)? ??? t? ? ???t? #data mov rn, a bytes: 1 cycles: 1 encoding: 1 1 1 1 1 r r r operation: mov (?n)?(a) ?(a) (a) mov rn,direct bytes: 2 cycles: 2 encoding: 1 0 1 0 1 r r r direct addr. operation: mov (?n)?(?ire?t) ?(?ire?t) (direct) mov rn,#data bytes: 2 cycles: 1 encoding: 0 1 1 1 1 r r r immediate data operation: mov (? n) ? ???t? ? ???t? #data stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 122 www.stcmcu.com mov direct, a bytes: 2 cycles: 1 encoding: 1 1 1 1 0 1 0 1 direct address operation: mov (?ire?t) ? (a) ? (a) (a) mov direct, rn bytes: 2 cycles: 2 encoding: 1 0 0 0 1 r r r direct address operation: mov (?ire?t) ? (?n) ? ( ?n) (rn) mov direct, direct bytes: 3 cycles: 2 encoding: 1 0 0 0 0 1 0 1 dir.addr. (src) operation: mov (?ire?t)? (?ire?t) ? ( ?ire?t) (direct) mov direct, @ri bytes: 2 cycles: 2 encoding: 1 0 0 0 0 1 1 i direct addr. operation: mov (?ire?t)?((?i)) ?((?i)) ((ri)) mov direct,#data bytes: 3 cycles: 2 encoding: 0 1 1 1 0 1 0 1 direct address operation: mov (?ire? t) ? ???t? ? ???t? #data mov @ri, a bytes: 1 cycles: 1 encoding: 1 1 1 1 0 1 1 i operation: mov ((?i)) ? (a) ? (a) (a) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 123 www.stcmcu.com mov @ri, direct bytes: 2 cycles: 2 encoding: 1 0 1 0 0 1 1 i direct addr. operation: mov ((?i)) ? (?ire?t) ? ( ?ire?t) (direct) mov @ri, #data bytes: 2 cycles: 1 encoding: 0 1 1 1 0 1 1 i immediate data operation: mov ((? i)) ? ???t? ? ???t? #data mov , function: move bit data description: the boolean variable indicated by the second operand is copied into the location specified by the first operand. one of the operands must be the carry flag; the other may be any directly addressable bit. no other register or flag is affected. example: the carry flag is originally set. the data present at input port 3 is 11000101b. the data previously written to output port 1 is 35h (00110101b). mov p1.3, c mov c, p3.3 mov p1.2, c will leave the carry cleared and change port 1 to 39h (00111001b). mov c,bit bytes: 2 cycles: 1 encoding: 1 0 1 0 0 0 1 1 bit address operation: mov (c) ? (bit) ? (bit) (bit) mov bit,c bytes: 2 cycles: 2 encoding: 1 0 0 1 0 0 1 0 bit address operation: mov (bit)? (c) ? (c) (c) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 124 www.stcmcu.com mov dptr , #data 16 function: load data pointer with a 16-bit constant description: the data pointer is loaded with the 16-bit constant indicated.the 16-bit constant is loaded into the second and third bytes of the instruction. the second byte (dph) is the high-order byte, while the third byte (dpl) holds the low-order byte. no flags are affected. this is the only instruction which moves 16 bits of data at once. example: the instruction, mov dptr, #1234h will load the value 1234h into the data pointer: dph will hold 12h and dpl will hold 34h. bytes: 3 cycles: 2 encoding: 1 0 0 1 0 0 0 0 immediate data 15-8 operation: mov (dpt? ) ? ???t? ? ???t? #data 15-0 dph dpl ? ??? t? ? ???t? 15-8 #data 7-0 movc a , @a+ function: move code byte description: the movc instructions load the accumulator with a code byte, or constant from program memory. the address of the byte fetched is the sum of the original unsigned eight-bit. accumulator contents and the contents of a sixteen-bit base register, which may be either the data pointer or the pc. in the latter case, the pc is incremented to the address of the following instruction before being added with the accumulator; otherwise the base register is not altered. sixteen-bit addition is performed so a carry-out from the low-order eight bits may propagate through higher-order bits. no flags are affected. example: a value between 0 and 3 is in the accumulator. the following instructions will translate the value in the accumulator to one of four values defimed by the db (define byte) directive. rel-pc: inc a movc a, @a+pc ret db 66h db 77h db 88h db 99h if the subroutine is called with the accumulator equal to 01h, it will return with 77h in the accumulator. the inc a before the movc instruction is needed to get around the ret instruction above the table. if several bytes of code separated the movc from the table, the corresponding number would be added to the accumulator instead. movc a,@a+dptr bytes: 1 cycles: 2 encoding: 1 0 0 1 0 0 1 1 operation: movc (a) ? ((a)?(dpt?)) ? ((a)?(dpt ?)) ((a)+(dptr)) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 125 www.stcmcu.com movc a,@a+pc bytes: 1 cycles: 2 encoding: 1 0 0 0 0 0 1 1 operation: movc (pc) ? (pc)?1 ? (pc)?1 (a) ? ((a)?(pc)) ? ((a)?(pc)) ((a)+(pc)) movx , function: move external description: the movx instructions transfer data between the accumulator and a byte of external data memory, hence the x appended to mov. there are two types of instructions, differing in whether they provide an eight-bit or sixteen-bit indirect address to the external data ram. in the first type, the contents of r0 or r1 in the current register bank provide an eight-bit address multiplexed with data on p0. eight bits are sufficient for external i/o expansion decoding or for a relatively small ram array. for somewhat larger arrays, any output port pins can be used to output higher-order address bits. these pins would be controlled by an output instruction preceding the movx. in the second type of movx instruction, the data pointer generates a sixteen-bit address. p2 outputs the high-order eight address bits (the contents of dph) while p0 multiplexes the low-order eight bits (dpl) with data. the p2 special function register retains its previous contents while the p2 output buffers are emitting the contents of dph. this form is faster and more efficient when accessing very large data arrays (up to 64k bytes), since no additional i nstructions are needed to set up the output ports. it is possible in some situations to mix the two movx types. a large ram array with its high-order address lines driven by p2 can be addressed via the data pointer, or with code to output high-order address bits to p2 followed by a movx instruction using r0 or r1. example: an external 256 byte ram using multiplexed address/data lines (e.g., an intel 8155 ram/ i/o/timer) is connected to the 8051 port 0. port 3 provides control lines for the external ram. ports 1 and 2 are used for normal i/o. registers 0 and 1 contain 12h and 34h. location 34h of the external ram holds the value 56h. the instruction sequence, movx a, @r1 movx @r0, a copies the value 56h into both the accumulator and external ram location 12h. movx a,@ri bytes: 1 cycles: 2 encoding: 1 1 1 0 0 0 1 i operation: movx (a) ? ((?i)) ? (( ?i)) ((ri)) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 126 www.stcmcu.com movx a,@dptr bytes: 1 cycles: 2 encoding: 1 1 1 0 0 0 0 0 operation: movx (a) ? ((dpt?)) ? ((dpt ?)) ((dptr)) movx @ri, a bytes: 1 cycles: 2 encoding: 1 1 1 1 0 0 1 i operation: movx ((?i))? (a) ? (a) (a) movx @dptr, a bytes: 1 cycles: 2 encoding: 1 1 1 1 0 0 0 0 operation: movx (dpt?)?(a) ?(a) (a) mul ab function: multiply description: mul ab multiplies the unsigned eight-bit integers in the accumulator and register b. the low-order byte of the sixteen-bit product is left in the accumulator, and the high-order byte in b. if the product is greater than 255 (0ffh) the overflow flag is set; otherwise it is cleared. the carry flag is always cleared example: originally the accumulator holds the value 80 (50h). register b holds the value 160 (0a0h). the instruction, mul ab will give the product 12,800 (3200h), so b is changed to 32h (00110010b) and the accumulator is cleared. the overflow flag is set, carry is cleared. bytes: 1 cycles: 4 encoding: 1 0 1 0 0 1 0 0 operation: mul (a) 7-0 ? (a) ?(?) (a)(b) (b) 15-8 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 127 www.stcmcu.com nop function: no operation description: execution continues at the following instruction. other than the pc, no registers or flags are affected. example: it is desired to produce a low-going output pulse on bit 7 of port 2 lasting exactly 5 cycles. a simple setb/clr sequence would generate a one-cycle pulse, so four additional cycles must be inserted. this may be done (assuming no interrupts are enabled) with the instruction sequence. clr p2.7 nop nop nop nop setb p2.7 bytes: 1 cycles: 1 encoding: 0 0 0 0 0 0 0 0 operation: nop (pc) ? (pc)?1 (pc)+1 orl , function: logical-or for byte variables description: orl performs the bitwise logical-or operation between the indicated variables, storing the results in the destination byte. no flags are affected. the two operands allow six addressing mode combinations. when the destination is the accumulator, the source can use register, direct, register-indirect, or immediate addressing; when the destination is a direct address, the source can be the accumulator or immediate data. note: when this instruction is used to modify an output port, the value used as the original port data will be read from the output data latch, not the input pins. example: if the accumulator holds 0c3h (11000011b) and r0 holds 55h (01010101b) then the instruction, orl a, r0 will leave the accumulator holding the value 0d7h (11010111b). when the destination is a directly addressed byte, the instruction can set combinations of bits in any ram location or hardware register. the pattern of bits to be set is determined by a mask byte, which may be either a constant data value in the instruction or a variable computed in the accumulator at run-time.the instruction, orl p1, #00110010b will set bits 5,4, and 1of output port 1. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 128 www.stcmcu.com orl a,rn bytes: 1 cycles: 1 encoding: 0 1 0 0 1 r r r operation: orl (a) ? (a) ? (a) (a) (rn) orl a,direct bytes: 2 cycles: 1 encoding: 0 1 0 0 0 1 0 1 direct address operation: orl (a)? (a) ? (a) (a) (direct) orl a,@ri bytes: 1 cycles: 1 encoding: 0 1 0 0 0 1 1 i operation: orl (a)? (a) ? (a) (a) ((ri)) orl a,#data bytes: 2 cycles: 1 encoding: 0 1 0 0 0 1 0 0 immediate data operation: orl (a)? (a) ? (a) (a) #data orl direct, a bytes: 2 cycles: 1 encoding: 0 1 0 0 0 0 1 0 direct address operation: orl (?ire?t)? (?ire?t) ? ( ?ire?t) (direct) (a) orl direct, #data bytes: 3 cycles: 2 encoding: 0 1 0 0 0 0 1 1 direct address immediate data operation: orl (?ire?t) ? (?ire?t) ? ( ?ire?t) (direct) #data stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 129 www.stcmcu.com orl c, function: logical-or for bit variables description: set the carry flag if the boolean value is a logical 1; leave the carry in its current state otherwise. a slash ( / ) preceding the operand in the assembly language indicates that the logical complement of the addressed bit is used as the source value, but the source bit itself is not affected. no other flags are affected. example: set the carry flag if and only if p1.0 = 1, acc. 7 = 1, or ov = 0: mov c, p1.0 ;load carry with input pin p10 orl c, acc.7 ;or carry with the acc.bit 7 orl c, /ov ;or carry with the inverse of ov orl c, bit bytes: 2 cycles: 2 encoding: 0 1 1 1 0 0 1 0 bit address operation: orl (c) ? (c) ? (c) (c) (bit) orl c, /bit bytes: 2 cycles: 2 encoding: 1 0 1 0 0 0 0 0 bit address operation: orl (c) ? (c) ? (c) (c) (bit) pop direct function: pop from stack description: the contents of the internal ram location addressed by the stack pointer is read, and the stack pointer is decremented by one. the value read is then transferred to the directly addressed byte indicated. no flags are affected. example: the stack pointer originally contains the value 32h, and internal ram locations 30h through 32h contain the values 20h, 23h, and 01h, respectively. the instruction sequence, pop dph pop dpl will leave the stack pointer equal to the value 30h and the data pointer set to 0123h. at this point the instruction, pop sp will leave the stack pointer set to 20h. note that in this special case the stack pointer was decremented to 2fh before being loaded with the value popped (20h). bytes: 2 cycles: 2 encoding: 1 1 0 1 0 0 0 0 direct address operation: pop (diect) ? ((sp)) ((sp)) (sp) ? (sp) - 1 ? (sp) - 1 (sp) - 1 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 130 www.stcmcu.com push direct function: push onto stack description: the stack pointer is incremented by one. the contents of the indicated variableis then copied into the internal ram location addressed by the stack pointer. otherwise no flags are affected. example: on entering interrupt routine the stack pointer contains 09h. the data pointer holds the value 0123h. the instruction sequence, push dpl push dph will leave the stack pointer set to 0bh and store 23h and 01h in internal ram locations 0ah and 0bh, respectively. bytes: 2 cycles: 2 encoding: 1 1 0 0 0 0 0 0 direct address operation: push (sp) ? (sp) ? 1 ? (sp) ? 1 (sp) + 1 ((sp)) ? (?ire?t) ? ( ?ire?t) (direct) ret function: return from subroutine description: ret pops the high-and low-order bytes of the pc successively from the stack, decrementing the stack pointer by two. program execution continues at the resulting address, generally the instruction immediately following an acall or lcall. no flags are affected. example: the stack pointer originally contains the value 0bh. internal ram locations 0ah and 0bh contain the values 23h and 01h, respectively. the instruction, ret will leave the stack pointer equal to the value 09h. program execution will continue at location 0123h. bytes: 1 cycles: 2 encoding: 0 0 1 0 0 0 1 0 operation: ret (pc 15-8 ) ? ((sp)) ? ((sp)) ((sp)) (sp) ? (sp) -1 ? (sp) -1 (sp) -1 (pc 7-0 ) ? ((sp)) ? ((sp)) ((sp)) (sp) ? (sp) -1 ? (sp) -1 (sp) -1 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 131 www.stcmcu.com reti function: return from interrupt description: reti pops the high- and low-order bytes of the pc successively from the stack, and restores the interrupt logic to accept additional interrupts at the same priority level as the one just processed. the stack pointer is left decremented by two. no other registers are affected; the psw is not automatically restored to its pre-interrupt status. program execution continues at the resulting address, which is generally the instruction immediately after the point at which the interrupt request was detected. if a lower- or same-level interrupt had been pending when the reti instruction is executed, that one instruction will be executed before the pending interrupt is processed. example: the stack pointer originally contains the value 0bh. an interrupt was detected during the instruction ending at location 0122h. internal ram locations 0ah and 0bh contain the values 23h and 01h, respectively. the instruction, reti will leave the stack pointer equal to 09h and return program execution to location 0123h. bytes: 1 cycles: 2 encoding: 0 0 1 1 0 0 1 0 operation: reti (pc 15-8 ) ? ((sp)) ? ((sp)) ((sp)) (sp) ? (sp) -1 ? (sp) -1 (sp) -1 (pc 7-0 ) ? ((sp)) ? ((sp)) ((sp)) (sp) ? (sp) -1 ? (sp) -1 (sp) -1 rl a function: rotate accumulator left description: the eight bits in the accumulator are rotated one bit to the left. bit 7 is rotated into the bit 0 position. no flags are affected. example: the accumulator holds the value 0c5h (11000101b). the instruction, rl a leaves the accumulator holding the value 8bh (10001011b) with the carry unaffected. bytes: 1 cycles: 1 encoding: 0 0 1 0 0 0 1 1 operation: rl (a n +1 ) ? (a ? (a (a n ) n = 0-6 (a 0 ) ? (a ? (a (a 7 ) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 132 www.stcmcu.com rlc a function: rotate accumulator left through the carry flag description: the eight bits in the accumulator and the carry flag are together rotated one bit to the left. bit 7 moves into the carry flag; the original state of the carry flag moves into the bit 0 position. no other flags are affected. example: the accumulator holds the value 0c5h (11000101b), and the carry is zero. the instruction, rlc a leaves the accumulator holding the value 8bh (10001011b) with the carry set. bytes: 1 cycles: 1 encoding: 0 0 1 1 0 0 1 1 operation: rlc (a n +1 ) ? (a ? (a (a n ) n = 0-6 (a 0 ) ? (c) ? (c) (c) (c) ? (a ? (a (a 7 ) rr a function: rotate accumulator right description: the eight bits in the accumulator are rotated one bit to the right. bit 0 is rotated into the bit 7 position. no flags are affected. example: the accumulator holds the value 0c5h (11000101b). the instruction, rr a leaves the accumulator holding the value 0e2h (11100010b) with the carry unaffected. bytes: 1 cycles: 1 encoding: 0 0 0 0 0 0 1 1 operation: rr (a n ) ? (a ? (a (a n +1) n = 0 - 6 (a 7 ) ? (a ? (a (a 0 ) rrc a function: rotate accumulator right through the carry flag description: the eight bits in the accumulator and the carry flag are together rotated one bit to the right. bit 0 moves into the carry flag; the original value of the carry flag moves into the bit 7 position.no other flags are affected. example: the accumulator holds the value 0c5h (11000101b), and the carry is zero. the instruction, rrc a leaves the accumulator holding the value 62h (01100010b) with the carry set. bytes: 1 cycles: 1 encoding: 0 0 0 1 0 0 1 1 operation: rrc (a n +1 ) ? (a ? (a (a n ) n = 0-6 (a 7 ) ? (c) ? (c) (c) (c) ? (a ? (a (a 0 ) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 133 www.stcmcu.com setb function: set bit description: setb sets the indicated bit to one. setb can operate on the carry flag or any directly addressable bit. no other flags are affected example: the carry flag is cleared. output port 1 has been written with the value 34h (00110100b). the instructions, setb c setb p1.0 will leave the carry flag set to 1 and change the data output on port 1 to 35h (00110101b). setb c bytes: 1 cycles: 1 encoding: 1 1 0 1 0 0 1 1 operation: setb (c) ? 1 ? 1 1 setb bit bytes: 2 cycles: 1 encoding: 1 1 0 1 0 0 1 0 bit address operation: setb (bit) ? 1 ? 1 1 sjmp rel function: short jump description: program control branches unconditionally to the address indicated. the branch destination is computed by adding the signed displacement in the second instruction byte to the pc, after incrementing the pc twice. therefore, the range of destinations allowed is from 128bytes preceding this instruction to 127 bytes following it. example: the label reladr is assigned to an instruction at program memory location 0123h. the instruction, sjmp reladr will assemble into location 0100h. after the instruction is executed, the pc will contain the value 0123h. ( note: under the above conditions the instruction following sjmp will be at 102h.therefore, the displacement byte of the instruction will be the relative offset (0123h - 0102h) = 21h. put another way, an sjmp with a displacement of 0feh would be an one-instruction infinite loop). bytes: 2 cycles: 2 encoding: 1 0 0 0 0 0 0 0 rel. address operation: sjmp ( pc ) ? (pc)?2 ? (pc)?2 (pc)+2 (pc) ? (pc)?rel ? (pc)?rel (pc)+rel stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 134 www.stcmcu.com subb a, function: subtract with borrow description: subb subtracts the indicated variable and the carry flag together from the accumulator, leaving the result in the accumulator. subb sets the carry (borrow)flag if a borrow is needed for bit 7, and clears c otherwise.(if c was set before executing a subb instruction, this indicates that a borrow was needed for the previous step in a multiple precision subtraction, so the carry is subtracted from the accumulator along with the source operand).ac is set if a borrow is needed for bit 3, and cleared otherwise. ov is set if a borrow is needed into bit 6, but not into bit 7, or into bit 7, but not bit 6. when subtracting signed integers ov indicates a negative number produced when a negative value is subtracted from a positive value, or a positive result when a positive number is subtracted from a negative number. the source operand allows four addressing modes: register, direct, register-indirect, or immediate. example: the accumulator holds 0c9h (11001001b), register 2 holds 54h (01010100b), and the carry flag is set. the instruction, subb a, r2 will leave the value 74h (01110100b) in the accumulator, with the carry flag and ac cleared but ov set. notice that 0c9h minus 54h is 75h. the difference between this and the above result is due to the carry (borrow) flag being set before the operation. if the state of the carry is not known before starting a single or multiple-precision subtraction, it should be explicitly cleared by a clr c instruction. subb a, rn bytes: 1 cycles: 1 encoding: 1 0 0 1 1 r r r operation: subb (a) ? (a) - (c) - ( ?n) ? (a) - (c) - ( ?n) (a) - (c) - (rn) subb a, direct bytes: 2 cycles: 1 encoding: 1 0 0 1 0 1 0 1 direct address operation: subb (a) ? (a) - (c) - ( ?ire?t) ? (a) - (c) - ( ?ire?t) (a) - (c) - (direct) subb a, @ri bytes: 1 cycles: 1 encoding: 1 0 0 1 0 1 1 i operation: subb (a) ? (a) - (c) - (( ?i)) ? (a) - (c) - (( ?i)) (a) - (c) - ((ri)) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 135 www.stcmcu.com swap a function: swap nibbles within the accumulator description: swap a interchanges the low- and high-order nibbles (four-bit fields) of the accumulator (bits 3-0 and bits 7-4). the operation can also be thought of as a four-bit rotate instruction. no flags are affected. example: the accumulator holds the value 0c5h (11000101b). the instruction, swap a leaves the accumulator holding the value 5ch (01011100b). bytes: 1 cycles: 1 encoding: 1 1 0 0 0 1 0 0 operation: swap (a 3-0 ) (a 7-4 ) xch a, function: exchange accumulator with byte variable description: xch loads the accumulator with the contents of the indicated variable, at the same time writing the original accumulator contents to the indicated variable. the source/destination operand can use register, direct, or register-indirect addressing. example: r0 contains the address 20h. the accumulator holds the value 3fh (00111111b). internal ram location 20h holds the value 75h (01110101b). the instruction, xch a, @r0 will leave ram location 20h holding the values 3fh (00111111b) and 75h (01110101b) in the accumulator. xch a, rn bytes: 1 cycles: 1 encoding: 1 1 0 0 1 r r r operation: xch (a) (rn) xch a, direct bytes: 2 cycles: 1 encoding: 1 1 0 0 0 1 0 1 direct address operation: xch (a) (direct) subb a, #data bytes: 2 cycles: 1 encoding: 1 0 0 1 0 1 0 0 immediate data operation: subb (a) ? (a) - (c) - ???t? ? (a) - (c) - ???t? (a) - (c) - #data stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 136 www.stcmcu.com xch a, @ri bytes: 1 cycles: 1 encoding: 1 1 0 0 0 1 1 i operation: xch (a) ((ri)) xchd a, @ri function: exchange digit description: xchd exchanges the low-order nibble of the accumulator (bits 3-0), generally representing a hexadecimal or bcd digit, with that of the internal ram location indirectly addressed by the specified register. the high-order nibbles (bits 7-4) of each register are not affected. no flags are affected. example: r0 contains the address 20h. the accumulator holds the value 36h (00110110b). internal ram location 20h holds the value 75h (01110101b). the instruction, xchd a, @r0 will leave ram location 20h holding the value 76h (01110110b) and 35h (00110101b) in the accumulator. bytes: 1 cycles: 1 encoding: 1 1 0 1 0 1 1 i operation: xchd (a 3-0 ) (ri 3-0 ) xrl , function: logical exclusive-or for byte variables description: xrl performs the bitwise logical exclusive-or operation between the indicated variables, storing the results in the destination. no flags are affected. the two operands allow six addressing mode combinations.when the destination is the accumulator, the source can use register, direct, register-indirect, or immediate addressing; when the destination is a direct address,the source can be the accumulator or immediate data. ( note : when this instruction is used to modify an output port, the value used as the original port data will be read from the output data latch, not the input pins.) example: if the accumulator holds 0c3h (11000011b) and register 0 holds 0aah (10101010b) then the instruction, xrl a, r0 will leave the accumulator holding the vatue 69h (01101001b). when the destination is a directly addressed byte, this instruction can complement combinna- tion of bits in any ram location or hardware register. the pattern of bits to be complemented is then determined by a mask byte, either a constant contained in the instruction or a variable computed in the accumulator at run-time. the instruction, xrl p1, #00110001b will complement bits 5,4 and 0 of outpue port 1. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 137 www.stcmcu.com xrl a, rn bytes: 1 cycles: 1 encoding: 0 1 1 0 1 r r r operation: xrl (a) ? (a) ? (a) (rn) xrl a, direct bytes: 2 cycles: 1 encoding: 0 1 1 0 0 1 0 1 direct address operation: xrl (a) ? (a) ? (a) (direct) xrl a, @ri bytes: 1 cycles: 1 encoding: 0 1 1 0 0 1 1 i operation: xrl (a) ? (a) ? (a) ((ri)) xrl a, #data bytes: 2 cycles: 1 encoding: 0 1 1 0 0 1 0 0 immediate data operation: xrl (a) ? (a) ? (a) #data xrl direct, a bytes: 2 cycles: 1 encoding: 0 1 1 0 0 0 1 0 direct address operation: xrl (?ire?t) ? (?ire?t) ? (?ire?t) (a) xrl direct, #dataw bytes: 3 cycles: 2 encoding: 0 1 1 0 0 0 1 1 direct address immediate data operation: xrl (?ire?t) ? (?ire?t) ? (?ire?t) # data stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 138 www.stcmcu.com stc12c5a60s2 series support 10 interrupt sources with four priority levels. the 10 interrupt sources are external interrupt 0( int0 ), timer 0 interrrupt, external interrupt 1( int1 ), timer 1 interrrupt, serial port 1(uart1) interrupt, adc interrupt, low voltage detection (lvd) interrupt, pca interrupt, serial port 2(uart2) interrupt and spi interrupt. each interrupt source has one or more associated interrupt-request flag(s) in sfrs. associating with each interrupt vector, the interrupt sources can be individually enabled or disabled by setting or clearing a bit (interrupt enalbe control bit) in the sfrs ie, ccon and ie2. however, interrupts must first be globally enabled by setting the ea bit (ie.7) to logic 1 before the individual interrupt enables are recognized. setting the ea bit to logic 0 disables all interrupt sources regardless of the individual interrupt-enable settings . if interrupts are enabled for the source, an interrupt request is generated when the interrupt-request flag is set. as soon as execution of the current instruction is complete, the cpu generates an lcall to a predetermined address to begin execution of an interrupt service routine (isr). each isr must end with an reti instruction, which returns pr o gram execution to the next instruction that would have been executed if the interrupt request had not occurred. if interrupts are not enabled, the interrupt- pending flag is ignored by the hardware and program execution continues as normal. (the interrupt- pending flag is set to logic 1 regardless of the interrupts enable/disable state.) each interrupt source has two corresponding bits to represent its priority. one is located in sfr named iph and other in ip register. higher-priority interrupt will be not interrupted by lower-priority interrupt request. if two interrupt requests of different priority levels are received simultaneously, the request of higher priority is serviced. if interrupt requests of the same priority level are received simultaneously, an internal polling sequence determine which request is serviced. the following table shows the internal polling sequence in the same priority level and the interrupt vector address. chapter 6. interrupt system interrupt sources, vector address, priority and polling sequence table interrupt source interrupt vector address priority within level interrupt priority setting(iph, ip) priority 0 (lowest) priority 1 priority 2 priority 3 (highest) interrupt request interrupt enable control bit external interrupt 0 ( int0 ) 0003h 0(highest) px0h,px0 0,0 0,1 1,0 1,1 ie0 ex0/ea timer 0 000bh 1 pt0h,pt0 0,0 0,1 1,0 1,1 tf0 et0/ea external interrupt 1 ( int1 ) 0013h 2 px1h,px1 0,0 0,1 1,0 1,1 ie1 ex1/ea timer1 001bh 3 pt1h,pt1 0,0 0,1 1,0 1,1 tf1 et1/ea serial port 0023h 4 psh,ps 0,0 0,1 1,0 1,1 ri+ti es/ea adc 002bh 5 padch,padc 0,0 0,1 1,0 1,1 adc_flag eadc/ea lvd 0033h 6 plvdh,plvd 0,0 0,1 1,0 1,1 lvd elvd/ea pca 003bh 7 ppcah,ppca 0,0 0,1 1,0 1,1 cf+ccf0 + ccf1 (ecf+eccf0 +eccf1)/ea uart2 (s2) 0043h 8 ps2h,ps2 0,0 0,1 1,0 1,1 s2ti+s2ri es2/ea spi 004bh 9(lowest) pspih,pspi 0,0 0,1 1,0 1,1 spif espi/ea stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 139 www.stcmcu.com in c language program. the interrupt polling sequence number is equal to interrupt number, for example, void int0_routine(void) interrupt 0; void timer0_rountine(void) interrupt 1; void int1_routine(void) interrupt 2; void timer1_rountine(void) interrupt 3; void uart_routine(void) interrupt 4; void adc_routine(void) interrupt 5; void lvd_routine(void) interrupt 6; void pca_routine(void) interrupt 7; void uart2_routine(void) interrupt 8; void spi_routine(void) interrupt 9; stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 140 www.stcmcu.com 6.1 interrupt structure the interrupt structure of stc12c5a60s2 series is shown as below. interrupt polling sequence ip,ip2,iph,ip2h registers ie, ie2 register elvd es2 espi global enable ea tf0 tf1 ri ti lvdf adc_flag cf ecf ccf0 eccf0 ccf1 eccf1 s2ri s2ti ie0 ie1 eadc es et1 ex1 et0 ex0 spif tcon.0/it0=1 tcon.0/it0=0 tcon.2/it1=1 tcon.2/it1=0 ea interrupt enable conterol registers figure stc12c5a60s2 series interrupt structure diagram int0 int1 uart1/s1 uart2/s2 1,0 1,1 px0h, px0 0,0 0,1 high low 1,0 1,1 0,0 0,1 1,0 1,1 0,0 0,1 1,0 1,1 0,0 0,1 1,0 1,1 0,0 0,1 1,0 1,1 0,0 0,1 1,0 1,1 0,0 0,1 1,0 1,1 0,0 0,1 1,0 1,1 0,0 0,1 1,0 1,1 0,0 0,1 pt0h, pt0 px1h, px1 pt1h, pt1 psh, ps padch, padc plvdh, plvd ppcah, ppca ps2h, ps2 pspih, pspi ea interrupt priority conterol registers highest priority level interrupt lowest priority level interrupt stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 141 www.stcmcu.com the external interrupts int0 and int1 can each be either level-activated or transition-activated, depending on bits it0 and it1 in register tcon. the flags that actually generate these interrupts are bits ie0 and ie1 in tcon. when an external interrupt is generated, the flag that generated it is cleared by the hardware when the service routine is vectored to if and only if the interrupt was transition Cactivated, otherwise the external requesting source is what controls the request flag, rather than the on-chip hardware. the timer 0 and timer1 interrupts are generated by tf0 and tf1, which are set by a rollover in their respective timer/counter registers in most cases. when a timer interrupt is generated, the flag that generated it is cleared by the on-chip hardware when the service routine is vectored to. the serial port interrupt is generated by the logical or of ri and ti. neither of these flags is cleared by hardware when the service routine is vectored to. in fact, the service routine will normally have to determine whether it was ri and ti that generated the interrupt, and the bit will have to be cleared by software. the adc interrupt is generated by the flag C adc_flag. it should be cleared by software. the low voltage detect interrupt is generated by the flag C lvdf(pcon.5) in pcon register. it should be cleared by software. the pca interrupt is generated by the logical or of cf, ccf0 ~ ccf1. the service routine should poll cf and ccf0 ~ ccf1 to determine which one to request service and it will be cleared by software. the secondary serial port interrupt is generated by the logical or of s2ri and s2ti. neither of these flags is cleared by hardware when the service routine is vectored to. the service routine should poll s2ri and s2ti to determine which one to request service and it will be cleared by software. the spi interrupt is generated by the flag spif. it can only be cleared by writing a 1 to spif bit in software. all of the bits that generate interrupts can be set or cleared by software, with the same result as though it had been set or cleared by hardware. in other words, interrupts can be generated or pending interrupts can be canceled in software. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 142 www.stcmcu.com 6.2 interrupt register symbol description address bit address and symbol msb lsb value after power-on or reset ie interrupt enable a8h ea elvd eadc es et1 ex1 et0 ex0 0000 0000b ip interrupt priority low b8h ppca plvd padc ps pt1 px1 pt0 px0 0000 0000b iph interrupt priority high b7h ppcah plvdh padch psh pt1h px1h pt0h px0h 0000 0000b ie2 interrupt enable 2 afh - - - - - - espi es2 xxxx xx00b ip2 2rd interrupt priority low register b5h - - - - - - pspi ps2 xxxx xx00b ip2h 2rd interrupt priority low register b6h - - - - - - pspih ps2h xxxx xx00b tcon timer control 88h tf1 tr1 tf0 tr0 ie1 it1 ie0 it0 0000 0000b scon serial control 98h sm0/fe sm1 sm2 ren tb8 rb8 ti ri 0000 0000b auxr auxiliary register 8eh t0x12 t1x12 uart_m0x6 brtr s2smod brtx12 extram s1brs 0000 0000b pcon power control 87h smod smod0 lvdf pof gf1 gf0 pd idl 0001 0000b wake_clko clk_output power down wake-up control register 8fh pcawakeup rxd_pin_ie t1_pin_ie t0_pin_ie lvd_wake brtclko t1clko t0clko 0000 0000b adc_contr adc control bch adc_power speed1 speed0 adc_flag adc_start chs2 chs1 chs0 0000 0000b ccon pca control register d8h cf cr - - - - ccf1 ccf0 00xx xx00b cmod pca mode register d9h cidl - - - cps2 cps1 cps0 ecf 00xx 0000b ccapm0 pca module 0 mode register dah - ecom0 capp0 capn0 mat0 tog0 pwm0 eccf0 x000 0000b ccapm1 pca module 1 mode register dbh - ecom1 capp1 capn1 mat1 tog1 pwm1 eccf1 x000 0000b spstat spi status register cdh spif wcol - - - - - - 00xx xxxxb stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 143 www.stcmcu.com ie: interrupt enable rsgister (bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ie a8h name ea elvd eadc es et1 ex1 et0 ex0 enable bit = 1 enables the interrupt . enable bit = 0 disables it . ea (ie.7): disables all interrupts. if ea = 0,no interrupt will be acknowledged. if ea = 1, each interrupt source is individually enabled or disabled by setting or clearing its enable bit. elvd (ie.6): low volatge detection interrupt enable bit. if elvd = 0, low voltage detection interrupt will be diabled. if elvd = 1, low voltage detection interrupt is enabled. eadc (ie.5): adc interrupt enable bit. if eadc = 0, adc interrupt will be diabled. if eadc = 1, adc interrupt is enabled. es (ie.4): serial port 1 (uart1) interrupt enable bit. if es = 0, uart1 interrupt will be diabled. if es = 1, uart1 interrupt is enabled. et1 (ie.3): timer 1 interrupt enable bit. if et1 = 0, timer 1 interrupt will be diabled. if et1 = 1, timer 1 interrupt is enabled. ex1 (ie.2): external interrupt 1 enable bit. if ex1 = 0, external interrupt 1 will be diabled. if ex1 = 1, external interrupt 1 is enabled. et0 (ie.1): timer 0 interrupt enable bit. if et0 = 0, timer 0 interrupt will be diabled. if et0 = 1, timer 0 interrupt is enabled. ex0 (ie.0): external interrupt 0 enable bit. if ex0 = 0, external interrupt 0 will be diabled. if ex0 = 1, external interrupt 0 is enabled. 1. interrupt enable control registers ie and ie2 ie2: interrupt enable 2 rsgister (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ie2 afh name - - - - - - espi es2 espi (ie2.1): spi interrupt enalbe bit. if espi = 0, spi interrupt will be diabled. if espi = 1, spi interrupt is enabled. es2 (ie.0): serial port 2 (uart2) interrupt enable bit. if es2 = 0, uart2 interrupt will be diabled. if es2 = 1, uart2 interrupt is enabled. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 144 www.stcmcu.com iph: interrupt priority high register (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 iph b7h name ppcah plvdh padch psh pt1h px1h pt0h px0h ip: interrupt priority register (bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ip b8h name ppca plvd padc ps pt1 px1 pt0 px0 ppcah, ppca: pca interrupt priority control bits. if ppcah=0 and ppca=0, pca interrupt is assigned lowest priority (priority 0). if ppcah=0 and ppca=1, pca interrupt is assigned lower priority (priority 1). if ppcah=1 and ppca=0, pca interrupt is assigned higher priority (priority 2). if ppcah=1 and ppca=1, pca interrupt is assigned highest priority (priority 3). plvdh, plvd: low voltage detection interrupt priority control bits. if plvdh=0 and plvd=0, low voltage detection interrupt is assigned lowest priority(priority 0). if plvdh=0 and plvd=1, low voltage detection interrupt is assigned lower priority(priority 1). if plvdh=1 and plvd=0, low voltage detection interrupt is assigned higher priority(priority 2). if plvdh=1 and plvd=1,low voltage detection interrupt is assigned highest priority(priority 3). padch, padc: adc interrupt priority control bits. if padch=0 and padc=0, adc interrupt is assigned lowest priority (priority 0). if padch=0 and padc=1, adc interrupt is assigned lower priority (priority 1). if padch=1 and padc=0, adc interrupt is assigned higher priority (priority 2). if padch=1 and padc=1, adc interrupt is assigned highest priority (priority 3). psh, ps: serial port 1 (uart1) interrupt priority control bits. if psh=0 and ps=0, uart1 interrupt is assigned lowest priority (priority 0). if psh=0 and ps=1, uart1 interrupt is assigned lower priority (priority 1). if psh=1 and ps=0, uart1 interrupt is assigned higher priority (priority 2). if psh=1 and ps=1, uart1 interrupt is assigned highest priority (priority 3). pt1h, pt1: timer 1 interrupt priority control bits. if pt1h=0 and pt1=0, timer 1 interrupt is assigned lowest priority (priority 0). if pt1h=0 and pt1=1, timer 1 interrupt is assigned lower priority (priority 1). if pt1h=1 and pt1=0, timer 1 interrupt is assigned higher priority (priority 2). if pt1h=1 and pt1=1, timer 1 interrupt is assigned highest priority (priority 3). px1h, px1: external interrupt 1 priority control bits. if px1h=0 and px1=0, external interrupt 1 is assigned lowest priority (priority 0). if px1h=0 and px1=1, external interrupt 1 is assigned lower priority (priority 1). if px1h=1 and px1=0, external interrupt 1 is assigned higher priority (priority 2). if px1h=1 and px1=1, external interrupt 1 is assigned highest priority (priority 3). 2. interrupt priority control registers ip, ip2 and iph, ip2h each interrupt source of stc12c5a60s2 all can be individually programmed to one of four priority levels by setting or clearing the bits in special function registers ip or ip2 and iph or ip2h. a low-priority interrupt can itself be interrupted by a high-pority interrupt, but not by another low-priority interrupt. a high-priority interrupt cant be interrupted by any other interrupt source. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 145 www.stcmcu.com pt0h, pt0: timer 0 interrupt priority control bits. if pt0h=0 and pt0=0, timer 0 interrupt is assigned lowest priority (priority 0). if pt0h=0 and pt0=1, timer 0 interrupt is assigned lower priority (priority 1). if pt0h=1 and pt0=0, timer 0 interrupt is assigned higher priority (priority 2). if pt0h=1 and pt0=1, timer 0 interrupt is assigned highest priority (priority 3). px0h, px0: external interrupt 0 priority control bits. if px0h=0 and px0=0, external interrupt 0 is assigned lowest priority (priority 0). if px0h=0 and px0=1, external interrupt 0 is assigned lower priority (priority 1). if px0h=1 and px0=0, external interrupt 0 is assigned higher priority (priority 2). if px0h=1 and px0=1, external interrupt 0 is assigned highest priority (priority 3). ip2h: interrupt priority high register (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ip2h b6h name - - - - - - pspih ps2h ip2: interrupt priority register (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ip2 b5h name - - - - - - pspi ps2 pspih, pspi: spi interrupt priority control bits. if pspih=0 and pspi=0, spi interrupt is assigned lowest priority (priority 0). if pspih=0 and pspi=1, spi interrupt is assigned lower priority (priority 1). if pspih=1 and pspi=0, spi interrupt is assigned higher priority (priority 2). if pspih=1 and pspi=1, spi interrupt is assigned highest priority (priority 3). ps2h, ps2 : serial port 2 (uart2) interrupt priority control bits. if ps2h=0 and ps2=0, uart2 interrupt is assigned lowest priority (priority 0). if ps2h=0 and ps2=1, uart2 interrupt is assigned lower priority (priority 1). if ps2h=1 and ps2=0, uart2 interrupt is assigned higher priority (priority 2). if ps2h=1 and ps2=1, uart2 interrupt is assigned highest priority (priority 3). stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 146 www.stcmcu.com 3. tcon register : timer/counter control register (bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 tcon 88h name tf1 tr1 tf0 tr0 ie1 it1 ie0 it0 tf1: timer/counter 1 overflow flag. set by hardware on timer/counter 1 overflow. the flag can be cleared by software but is automatically cleared by hardware when processor vectors to the timer 1 interrupt routine. if tf1 = 0, no timer 1 overflow detected. if tf1 = 1, timer 1 has overflowed. tr1: timer/counter 1 run control bit . set/cleared by software to turn timer/counter on/off. if tr1 = 0, timer 1 disabled. if tr1 = 1, timer 1 enabled. tf0: timer/counter 0 overflow flag. set by hardware on timer/counter 0 overflow. the flag can be cleared by software but is automatically cleared by hardware when processor vectors to the timer 0 interrupt routine. if tf0 = 0, no timer 0 overflow detected. if tf0 = 1, timer 0 has overflowed. tr0: timer/counter 0 run control bit . set/cleared by software to turn timer/counter on/off. if tr0 = 0, timer 0 disabled. if tr0 = 1, timer 0 enabled. ie1: external interrupt 1 edge flag. set by hardware when external interrupt edge/level defined by it1 is detected. the flag can be cleared by software but is automatically cleared when the external interrupt 1 service routine has been processed. it1: external intenupt 1 type select bit. set/cleared by software to specify falling edge/low level triggered ex - ternal interrupt 1. if it1 = 0, int1 is low level triggered. if it1 = 1, int1 is edge triggered. ie0: external interrupt 0 edge flag. set by hardware when external interrupt edge/level defined by it0 is detected. the flag can be cleared by software but is automatically cleared when the external interrupt 0 service routine has been processed. it0: external intenupt 0 type select bit. set/cleared by software to specify falling edge/low level triggered ex - ternal interrupt 0. if it0 = 0, int0 is low level triggered. if it0 = 1, int0 is edge triggered. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 147 www.stcmcu.com 4. scon register : serial port 1 (uart1) control register (bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 scon 98h name sm0/fe sm1 sm2 ren tb8 rb8 ti ri fe: framing error bit. the smod0 bit must be set to enable access to the fe bit 0: the fe bit is not cleared by valid frames but should be cleared by software. 1: this bit set by the receiver when an invalid stop bit id detected. sm0,sm1 : serial port mode bit 0/1. sm0 sm1 description baud rate 0 0 8-bit shift register sysclk/12 0 1 8-bit uart variable 1 0 9-bit uart sysclk/64 or sysclk/32(smod=1) 1 1 9-bit uart variable sm2 : enable the automatic address recognition feature in mode 2 and 3. if sm2=1, ri will not be set unless the received 9th data bit is 1, indicating an address, and the received byte is a given or broadcast address. in mode1, if sm2=1 then ri will not be set unless a valid stop bit was received, and the received byte is a given or broadcast address. in mode 0, sm2 should be 0. ren : when set enables serial reception. tb8 : the 9th data bit which will be transmitted in mode 2 and 3. rb8 : in mode 2 and 3, the received 9th data bit will go into this bit. ti : transmit interrupt flag. set by hardware when a byte of data has been transmitted by uart0 (after the 8th bit in 8-bit uart mode, or at the beginning of the stop bit in 9-bit uart mode). when the uart0 in - terrupt is enabled, setting this bit causes the cpu to vector to the uart0 interrupt service routine. this bit must be cleared manually by software. ri : receive interrupt flag. set to 1 by hardware when a byte of data has been received by uart0 (set at the stop bit sam-pling time). when the uart0 interrupt is enabled, setting this bit to 1 causes the cpu to vector to the uart0 interrupt service routine. this bit must be cleared manually by software. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 148 www.stcmcu.com 5. register related with lvd interrupt: power control register pcon (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 pcon 87h name smod smod0 lvdf pof gf1 gf0 pd idl smod: double baud rate control bit. 0 : disable double baud rate of the uart. 1 : enable double baud rate of the uart in mode 1,2,or 3. smod0: frame error select. 0 : scon.7 is sm0 function. 1 : scon.7 is fe function. note that fe will be set after a frame error regardless of the state of smod0. lvdf : pin low-voltage flag. once low voltage condition is detected (vcc power is lower than lvd voltage), it is set by hardware (and should be cleared by software). pof : power-on flag. it is set by power-off-on action and can only cleared by software. gf1 : general-purposed flag 1 gf0 : general-purposed flag 0 pd : power-down bit. idl : idle mode bit. ie: interrupt enable rsgister (bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ie a8h name ea elvd eadc es et1 ex1 et0 ex0 ea : disables all interrupts. if ea = 0,no interrupt will be acknowledged. if ea = 1, each interrupt source is individually enabled or disabled by setting or clearing its enable bit. elvd: low volatge detection interrupt enable bit. if elvd = 0, low voltage detection interrupt will be diabled. if elvd = 1, low voltage detection interrupt is enabled. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 149 www.stcmcu.com 6. adc_contr: ad control register (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 adc_contr bch name adc_power speed1 speed0 adc_flag adc_start chs2 chs1 chs0 adc_power : when clear, shut down the power of adc bolck. when set, turn on the power of adc block. adc_flag : adc interrupt flag. it will be set by the device after the device has finished a conversion, and should be cleared by the user's software. adc_strat : adc start bit, which enable adc conversion.it will automatically cleared by the device after the device has finished the conversion ie: interrupt enable rsgister (bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ie a8h name ea elvd eadc es et1 ex1 et0 ex0 ea : disables all interrupts. if ea = 0,no interrupt will be acknowledged. if ea = 1, each interrupt source is individually enabled or disabled by setting or clearing its enable bit. eadc: adc interrupt enable bit. if eadc = 0, adc interrupt will be diabled. if eadc = 1, adc interrupt is enabled. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 150 www.stcmcu.com 7. register related with pca interrupt ccon: pca control register (bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ccon d8h name cf cr - - - - ccf1 ccf0 cf : pca counter overflow flag. set by hardware when the counter rolls over. cf flags an interrupt if bit ecf in cmod is set. cf may be set by either hardware or software but can only be cleared by software. cr : pca counter run control bit. set by software to turn the pca counter on. must be cleared by software to turn the pca counter off. ccf1: pca module 1 interrupt flag. set by hardware when a match or capture occurs. must be cleared by software. ccf0: pca module 0 interrupt flag. set by hardware when a match or capture occurs. must be cleared by software. cmod: pca mode register (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 cmod d9h name cidl - - - cps2 cps1 cps0 ecf cidl : counter idle control. cidl=0 programs the pca counter to continue functioning during idle mode. cidl=1 programs it to be gated off during idle. cps2 ~ cps0 : pca counter pulse select bits. 0 0 0 internal clock, fosc/12 0 0 1 internal clock, fosc/2 0 1 0 timer 0 overflow 0 1 1 external clock at eci/p1.2 pin 1 0 0 internal clock, fosc 1 0 1 internal clock, fosc/4 1 1 0 internal clock, fosc/6 1 1 1 internal clock, fsoc/8 ecf : pca enable counter overflow interrupt. ecf=1 enables cf bit in ccon to generate an interrupt. ccapmn register (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ccapm0 dah name - ecom0 capp0 capn0 mat0 tog0 pwm0 eccf0 ccapm1 dbh name - ecom1 capp1 capn1 mat1 tog1 pwm1 eccf1 ecomn : enable comparator. ecomn=1 enables the comparator function. cappn : capture positive, cappn=1 enables positive edge capture. capnn : capture negative, capnn=1 enables negative edge capture. matn : match. when matn=1, a match of the pca counter with this modules compare/capture register causes the ccfn bit in ccon to be set. togn : toggle. when togn=1, a match of the pca counter with this modules compare/capture register causes the cexn pin to toggle. pwmn : pulse width modulation. pwmn=1 enables the cexn pin to be used as a pulse width modulated output. eccfn : enable ccf interrupt. enables compare/capture flag ccfn in the ccon register to generate stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 151 www.stcmcu.com 8. register related with spi interrupt spstat: spi status control register (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 spstat cdh name spif wcol - - - - - - spif : spi transfer completion flag.when a serial transfer finishes, the spif bit is set and an interrupt is gener - ated if both the espi(ie.6) bit and the ea(ie.7) bit are set. if ss is an input and is driven low when spi is in master mode with ssig = 0, spif will also be set to signal the mode change.the spif is cleared in software by writing 1 to this bit. wcol: spi write collision flag. the wcol bit is set if the spi data register, spdat, is written during a data transfer. the wcol flag is cleared in software by writing 1 to this bit ie: interrupt enable rsgister (bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ie a8h name ea elvd eadc es et1 ex1 et0 ex0 ea : disables all interrupts. if ea = 0,no interrupt will be acknowledged. if ea = 1, each interrupt source is individually enabled or disabled by setting or clearing its enable bit. ie2: interrupt enable 2 rsgister (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ie2 afh name - - - - - - espi es2 espi: spi interrupt enable bit. if espi = 0, spi interrupt will be diabled. if espi = 1, spi interrupt is enabled. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 152 www.stcmcu.com 9. interrupt register related with power down wake-up: wake_clko (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 wake_clko 8fh name pcawakeup rxd_pin_ie t1_pin_ie t0_pin_ie lvd_wake brtcklo t1cklo t0cklo pcawakeup: when set and the associated-pca interrupt control registers is configured correctly, the cexn pin of pca function is enabled to wake up mcu from power-down state. rxd_pin_ie: when set and the associated-uart interrupt control registers is configured correctly, the rxd pin (p3.0) is enabled to wake up mcu from power-down state. t1_pin_ie : when set and the associated-timer1 interrupt control registers is configured correctly, the t1 pin (p3.5) is enabled to wake up mcu from power-down state. t0_pin_ie : when set and the associated-timer0 interrupt control registers is configured correctly, the t1 pin (p3.4) is enabled to wake up mcu from power-down state. lvd_wake: when set and the associated-lvd interrupt control registers is configured correctly, the cmpin pin is enabled to wake up mcu from power-down state. brtcklo : when set, p1.0 is enabled to be the clock output of baud-rate timer (brt). the clock rate is brg overflow rate divided by 2. t1cklo : when set, p3.5 is enabled to be the clock output of timer 1. the clock rate is timer 1overflow rate divided by 2. t0cklo : when set, p3.4 is enabled to be the clock output of timer 0. the clock rate is timer 0overflow rate divided by 2. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 153 www.stcmcu.com 6.3 interrupt priorities each interrupt source can also be individually programmed to one of four priority levels by setting or clearing the bits in special function registers ip or ip2 and iph or ip2h. a low-priority interrupt can itself be interrupted by a high-pority interrupt, but not by another low-priority interrupt. a high-priority interrupt cant be interrupted by any other interrupt source. if two requests of different priority levels are received simultaneously, the request of higher priority level is serviced. if requests of the same priority level are received simultaneously, an internal polling sequence determines which request is serviced. thus within each priority level there is a second priority structure determined by the polling sequence,as follows: source priority within level 0. ie0 (highest) 1. tf0 2. ie1 3. tf1 4. ri +tl 5. adc_flag 6. lvdf 7. pca 8. s2ri+s2ti 9. spif (lowest) note that the priority within level structure is only used to resolve simultaneous requests of the same prionty level. in c language program. the interrupt polling sequence number is equal to interrupt number, for example, void int0_routine(void) interrupt 0; void timer0_rountine(void) interrupt 1; void int1_routine(void) interrupt 2; void timer1_rountine(void) interrupt 3; void uart_routine(void) interrupt 4; void adc_routine(void) interrupt 5; void lvd_routine(void) interrupt 6; void pca_routine(void) interrupt 7; void uart2_routine(void) interrupt 8; void spi_routine(void) interrupt 9; stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 154 www.stcmcu.com 6.4 how interrupts are handled external interrupt pins and other interrupt sources are sampled at the rising edge of each instruction opcode fetch cycle. the samples are polled during the next instruction opcode fetch cycle . if one of the flags was in a set condition of the first cycle, the second cycle of polling cycles will find it and the interrupt system will generate an hardware lcall to the appropriate service routine as long as it is not blocked by any of the following conditions. block conditions : an interrupt of equal or higher priority level is already in progress. the current cycle(polling cycle) is not the final cycle in the execution of the instruction in progress. the instruction in progress is reti or any write to the ie, ie2, ip, ip2, iph and ip2h registers. the isp/iap activity is in progress. any of these four conditions will block the generation of the hardware lcall to the interrupt service routine. condition 2 ensures that the instruction in progress will be completed before vectoring into any service routine. condition 3 ensures that if the instruction in progress is reti or any access to ie, ie2, ip, ip2, iph or ip2h, then at least one or more instruction will be executed before any interrupt is vectored to. the polling cycle is repeated with the last clock cycle of each instruction cycle. note that if an interrupt flag is active but not being responded to for one of the above conditions, if the flag is not still active when the blocking condition is removed, the denied interrupt will not be serviced. in other words, the fact that the interrupt flag was once active but not being responded to for one of the above conditions, if the flag is not still active when the blocking condition is removed, the denied interrupt will not be serviced. the interrupt flag was once active but not serviced is not kept in memory. every polling cycle is new. ? ? ? ? note that if an interrupt of higher priority level goes active prior to the rising edge of the third machine cycle, then in accordance with the above rules it will be vectored to during fifth and sixth machine cycle, without any instruction of the lower priority routine having been executed. thus the processor acknowledges an interrupt request by executing a hardware-generated lcall to the appropriate servicing routine. in some cases it also clears the flag that generated the interrupt, and in other cases it doesnt. it never clears the serial port flags. this has to be done in the users software. it clears an external interrupt flag (ie0 or ie1) only if it was transition-activated. the hardware-generated lcall pushes the contents of the program counter onto the stack (but it does not save the psw) and reloads the pc with an address that depends on the source of the interrupt being vectored to, as shown be low. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 155 www.stcmcu.com source vector address ie0 0003h tf0 000bh ie1 0013h tf1 001bh ri+ti 0023h adc_flag 002bh lvdf 0033h pca 003bh s2ri+s2ti 0043h spif 004bh execution proceeds from that location until the reti instruction is encountered. the reti instruction informs the processor that this interrupt routine is no longer in progress, then pops the top two bytes from the stack and reloads the program counter. execution of the interrupted program continues from where it left off. note that a simple ret instruction would also have returned execution to the interrupted program, but it would have left the interrupt control system thinking an interrupt was still in progress. 6.5 external interrupts the external sources can be programmed to be level-activated or transition-activated by clearing or setting bit it1 or it0 in register tcon. if itx = 0, external interrupt x is triggered by a detected low at the intx pin. if itx=1, external interrupt x is edge-triggered. in this mode if successive samples of the intx pin show a high in one cycle and a low in the next cycle, interrupt request flag iex in tcon is set. flag bit iex then requests the interrupt. since the external interrupt pins are sampled once each machine cycle, an input high or low should hold for at least 12 system clocks to ensure sampling. if the external interrupt is transition-activated, the external source has to hold the request pin high for at least one machine cycle, and then hold it low for at least one machine cycle to ensure that the transition is seen so that interrupt request flag iex will be set. iex will be automatically cleared by the cpu when the service routine is called. if the external interrupt is level-activated, the external source has to hold the request active until the requested interrupt is actually generated. then it has to deactivate the request before the interrupt service routine is completed, or else another interrupt will be generated. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 156 www.stcmcu.com example : design an intrusion warning system using interrupts that sounds a 400hz tone for 1 second (using a loudspeaker connected to p1.7) whenever a door sensor connected to int0 makes a high-to-low transition. assembly language solution org 0 ljmp main ;3-byte instruction ljmp int0int ;ext 0 vector address org 000bh ;timer 0 vector ljmp t0int org 001bh ;timer 1 vector ljmp t1int org 0030h main: setb it0 ;negative edge activated mov tmod, #11h ;16-bit timer mode mov ie, #81h ;enable ext 0 only sjmp $ ;now relax ; int0int: mov r7, #20 ;20 ' 5000 us = 1 second setb tf0 ;force timer 0 interrupt setb tf1 ;force timer 1 interrupt setb et0 ;begin tone for 1 second setb et1 ;enable timer interrupts reti ; t0int: clr tr0 ;stop timer djnz r7, skip ;if not 20th time, exit clr et0 ;if 20th, disable tone clr et1 ;disable itself ljmp exit skip: mov th0, #high (-50000) ;0.05sec. delay mov tl0, #low (-5000) setb tr0 exit: reti ; t1int: clr tr1 mov th1, #high (-1250) ;count for 400hz mov tl1, #low (-1250) cpl p1.7 ;music maestro! setb tr1 reti end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 157 www.stcmcu.com c language solution #include /* sfr declarations */ sbit outbit = p1^7; /* use variable outbit to refer to p1.7 */ unsigned char r7; /* use 8-bit variable to represent r7 */ main( ) { it0 = 1; /* negative edge activated */ tmod = 0x11; /* 16-bit timer mode */ ie = 0x81; /* enable ext 0 only */ while(1); } void int0int(void) interrupt 0 { r7 = 20; /* 20 x 5000us = 1 second */ tf0 = 1; /* force timer 0 interrupt */ tf1 = 1; /* force timer 1 interrupt */ et0 = 1; /* begin tone for 1 second */ et1 = 1; /* enable timer 1 interrupts */ /* timer interrupts will do the work */ } void t0int(void) interrupt 1 { tr0 = 0; /* stop timer */ r7 = r7-1; /* decrement r7 */ if (r7 == 0) /* if 20 th time, */ { et0 = 0; /* disable itself */ et1 = 0; } else { th0 = 0x3c; /* 0.05 sec. delay */ tl0 = 0xb0; } } void t1int (void) interrupt 3 { tr0 = 0; th1 = 0xfb; /* count for 400hz */ tl1 = 0x1e; outbit = !outbit; /* music maestro! */ tr1 = 1; } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 158 www.stcmcu.com in the above assembly language solution, five distinct sections are the interrupt vector loactions, the main program, and the three interrupt service routines. all vector loacations contain ljmp instructions to the respective routines. the main program, starting at code address 0030h, contains only four instructions. setb it0 configures the door sensing interrupt input as negative-edge triggered. mov tmod, #11h configures both timers for mode 1, 16-bit timer mode. only the external 0 interrupt is enabled initially (mov ie,#81h), so a "door-open" condition is needed before any interrupt is accepted. finally, sjmp $ puts the main program in a do-nothing loop. when a door-open condition is sensed (by a high-to-low transition of int0), an external 0 interrupt is generated, int0int begins by putting the constant 20 in r7, then sets the overflow flags for both timers to force timer interrupts to occur. timer interrupt will only occur, however, if the respective bits are enabled in the ie register. the next two instructions (setb et0 and setb et1) enable timer interrupts. finally, int0int terminates with a reti to the main program. timer 0 creates the 1 second timeout, and timer 1 creates the 400hz tone. after int0int returns to the main program, timer interrupt are immediately generated (and accepted after one excution of sjmp $). because of the fixed polling sequence, the timer 0 interrupt is serviced first. a 1 second timeout is created by programming 20 repetitions of a 50,000 us timeout. r7 serves as the counter. nineteen times out of 20, t0int operates as follows. first, timer 0 is turned off and r7 is decremented. then, th0/tl is reload with -50,000, the timer is turned back on, and the interrupt is terminated. on the 20th timer 0 interrupt, r7 is decremented to 0 (1 second has elapsed). both timer interrupts are disabled(clr et0, clr et1)and the interrupt is terminated. no further timer interrupts will be generated until the next "door-open" condition is sensed. the 400hz tone is programmed using timer 1 interrupts, 400hz requires a period of 1/400 = 2,500 us or 1,250 high-time and 1,250 us low-time. each timer 1 isr simply puts -1250 in th1/tl1, complements the port bit driving the loudspeaker, then terminates. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 159 www.stcmcu.com 6.6 response time the int0 and int1 levels are inverted and latched into the interrupt flags ie0 and ie1 at rising edge of every syetem clock cycle. the timer 0 and timer 1 flags, tf0 and tf1, are set after which the timers overflow. the values are then polled by the circuitry at rising edge of the next system clock cycle. if a request is active and conditions are right for it to be acknowledged, a hardware subroutine call to the requested service routine will be the next instruction to be executed. the call itself takes six system clock cycles. thus, a minimum of seven complete system clock cycles elapse between activation of an external interrupt request and the beginning of execution of the first instruction of the service routine. a longer response time would result if the request is blocked by one of the four previously listed conditions. if an interrupt of equal or higher priority level is already in progress, the additional wait time obviously depends on the nature of the other interrupts service routine. if the instruction in progress is not in its final cycle, the additional wait time cannot be more than 3 cycles, since the longest instructions (lcall) are only 6 cycles long, and if the instruction in progress is reti or an access to ie or ip, the additional wait time cannot be more than 5 cycles (a maximum of one more cycle to complete the instruction in progress, plus 6 cycles to complete the next instruction if the instruction is lcall). thus, in a single-interrupt system, the response time is always more than 7 cycles and less than 12 cycles. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 160 www.stcmcu.com 1. demostrate external interrupt 0 triggered by falling edge c program /*-------------------------------------------------------------------------------*/ /* --- stc mcu international limited ----------------------------------*/ /* --- stc 1t series mcu ext0(falling edge) demo -----------------*/ /* --- mobile: (86)13922809991 ------------------------------------------*/ /* --- fax: 86-755-82905966 ----------------------------------------------*/ /* --- tel: 86-755-82948412 -----------------------------------------------*/ /* --- web: www.stcmcu.com -----------------------------------------*/ /* if you want to use the program or the program referenced in the */ /* article, please specify in which data and procedures from stc */ /*-------------------------------------------------------------------------------*/ #include "reg51.h" //external interrupt0 service routine void exint0() interrupt 0 //int0, interrupt 0 (location at 0003h) { } void main() { it0 = 1; //set int0 interrupt type (1:falling 0:low level) ex0 = 1; //enable int0 interrupt ea = 1; //open global interrupt switch while (1); } 6.7.1 external interrupt 0 ( int0 ) demo programs (c and asm) 6.7 demo programs about interrupts (c and assembly programs) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 161 www.stcmcu.com assembly program ;/*-------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited ----------------------------------*/ ;/* --- stc 1t series mcu ext0(falling edge) demo -----------------*/ ;/* --- mobile: (86)13922809991 ------------------------------------------*/ ;/* --- fax: 86-755-82905966 ----------------------------------------------*/ ;/* --- tel: 86-755-82948412 -----------------------------------------------*/ ;/* --- web: www.stcmcu.com -----------------------------------------*/ ;/* if you want to use the program or the program referenced in the */ ;/* article, please specify in which data and procedures from stc */ ;/*-------------------------------------------------------------------------------*/ ;----------------------------------------- ;interrupt vector table org 0000h ljmp main org 0003h ;int0, interrupt 0 (location at 0003h) ljmp exint0 ;----------------------------------------- org 0100h main: mov sp, #7fh ;initial sp setb it0 ;set int0 interrupt type (1:falling 0:low level) setb ex0 ;enable int0 interrupt setb ea ;open global interrupt switch sjmp $ ;----------------------------------------- ;external interrupt0 service routine exint0: reti ;----------------------------------------- end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 162 www.stcmcu.com 2. demostrate the power-down mode waked up by falling edge of external interrupt 0 c program /*------------------------------------------------------------------------------------*/ /* --- stc mcu international limited ---------------------------------------*/ /* --- stc 1t series mcu power-down wakeup by int0 demo -------*/ /* --- mobile: (86)13922809991 -----------------------------------------------*/ /* --- fax: 86-755-82905966 ---------------------------------------------------*/ /* --- tel: 86-755-82948412 ----------------------------------------------------*/ /* --- web: www.stcmcu.com -----------------------------------------------*/ /* if you want to use the program or the program referenced in the ------*/ /* article, please specify in which data and procedures from stc -------*/ /*-------------------------------------------------------------------------------------*/ #include "reg51.h" #include "intrins.h" //external interrupt0 service routine void exint0( ) interrupt 0 //int0, interrupt 0 (location at 0003h) { } void main() { it0 = 1; //set int0 interrupt type (1:falling 0:low level) ex0 = 1; //enable int0 interrupt ea = 1; //open global interrupt switch while (1) { int0 = 1; //ready read int0 port while (!int0); //check int0 _nop_(); _nop_(); pcon = 0x02; //mcu power down _nop_(); _nop_(); p1++; } } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 163 www.stcmcu.com assembly program /*------------------------------------------------------------------------------------*/ /* --- stc mcu international limited ---------------------------------------*/ /* --- stc 1t series mcu power-down wakeup by int0 demo -------*/ /* --- mobile: (86)13922809991 -----------------------------------------------*/ /* --- fax: 86-755-82905966 ---------------------------------------------------*/ /* --- tel: 86-755-82948412 ----------------------------------------------------*/ /* --- web: www.stcmcu.com -----------------------------------------------*/ /* if you want to use the program or the program referenced in the ------*/ /* article, please specify in which data and procedures from stc -------*/ /*-------------------------------------------------------------------------------------*/ ;----------------------------------------- ;interrupt vector table org 0000h ljmp main org 0003h ;int0, interrupt 0 (location at 0003h) ljmp exint0 ;----------------------------------------- org 0100h main: mov sp, #7fh ;initial sp setb it0 ;set int0 interrupt type (1:falling 0:low level) setb ex0 ;enable int0 interrupt setb ea ;open global interrupt switch loop: setb int0 ;ready read int0 port jnb int0, $ ;check int0 nop nop mov pcon, #02h ;mcu power down nop nop cpl p1.0 sjmp loop ;----------------------------------------- ;external interrupt0 service routine exint0: reti ;----------------------------------------- end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 164 www.stcmcu.com 1. demostrate external interrupt 1 triggered by falling edge c program /*-------------------------------------------------------------------------------*/ /* --- stc mcu international limited ----------------------------------*/ /* --- stc 1t series mcu ext1(falling edge) demo -----------------*/ /* --- mobile: (86)13922809991 ------------------------------------------*/ /* --- fax: 86-755-82905966 ----------------------------------------------*/ /* --- tel: 86-755-82948412 -----------------------------------------------*/ /* --- web: www.stcmcu.com -----------------------------------------*/ /* if you want to use the program or the program referenced in the */ /* article, please specify in which data and procedures from stc */ /*-------------------------------------------------------------------------------*/ #include "reg51.h" //external interrupt1 service routine void exint1() interrupt 2 //int1, interrupt 2 (location at 0013h) { } void main() { it1 = 1; //set int1 interrupt type (1:falling only 0:low level) ex1 = 1; //enable int1 interrupt ea = 1; //open global interrupt switch while (1); } 6.7.2 external interrupt 1 ( int1 ) demo programs (c and asm) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 165 www.stcmcu.com assembly program ;/*-------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited ----------------------------------*/ ;/* --- stc 1t series mcu ext1(falling edge) demo -----------------*/ ;/* --- mobile: (86)13922809991 ------------------------------------------*/ ;/* --- fax: 86-755-82905966 ----------------------------------------------*/ ;/* --- tel: 86-755-82948412 -----------------------------------------------*/ ;/* --- web: www.stcmcu.com -----------------------------------------*/ ;/* if you want to use the program or the program referenced in the */ ;/* article, please specify in which data and procedures from stc */ ;/*-------------------------------------------------------------------------------*/ ;----------------------------------------- ;interrupt vector table org 0000h ljmp main org 0013h ;int1, interrupt 2 (location at 0013h) ljmp exint1 ;----------------------------------------- org 0100h main: mov sp, #7fh ;initial sp setb it1 ;set int1 interrupt type (1:falling 0:low level) setb ex1 ;enable int1 interrupt setb ea ;open global interrupt switch sjmp $ ;----------------------------------------- ;external interrupt1 service routine exint1: reti ;----------------------------------------- end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 166 www.stcmcu.com 2. demostrate the power-down mode waked up by falling edge of external interrupt 1 c program /*------------------------------------------------------------------------------------*/ /* --- stc mcu international limited ---------------------------------------*/ /* --- stc 1t series mcu power-down wakeup by int1 demo -------*/ /* --- mobile: (86)13922809991 -----------------------------------------------*/ /* --- fax: 86-755-82905966 ---------------------------------------------------*/ /* --- tel: 86-755-82948412 ----------------------------------------------------*/ /* --- web: www.stcmcu.com -----------------------------------------------*/ /* if you want to use the program or the program referenced in the ------*/ /* article, please specify in which data and procedures from stc ------*/ /*-------------------------------------------------------------------------------------*/ #include "reg51.h" #include "intrins.h" //external interrupt0 service routine void exint1( ) interrupt 2 //int1, interrupt 2 (location at 0013h) { } void main() { it1 = 1; //set int1 interrupt type (1:falling 0:low level) ex1 = 1; //enable int1 interrupt ea = 1; //open global interrupt switch while (1) { int1 = 1; //ready read int1 port while (!int1); //check int1 _nop_(); _nop_(); pcon = 0x02; //mcu power down _nop_(); _nop_(); p1++; } } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 167 www.stcmcu.com assembly program /*------------------------------------------------------------------------------------*/ /* --- stc mcu international limited ---------------------------------------*/ /* --- stc 1t series mcu power-down wakeup by int1 demo -------*/ /* --- mobile: (86)13922809991 -----------------------------------------------*/ /* --- fax: 86-755-82905966 ---------------------------------------------------*/ /* --- tel: 86-755-82948412 ----------------------------------------------------*/ /* --- web: www.stcmcu.com -----------------------------------------------*/ /* if you want to use the program or the program referenced in the ------*/ /* article, please specify in which data and procedures from stc -------*/ /*-------------------------------------------------------------------------------------*/ ;----------------------------------------- ;interrupt vector table org 0000h ljmp main org 0013h ;int1, interrupt 2 (location at 0013h) ljmp exint1 ;----------------------------------------- org 0100h main: mov sp,#7fh ;initial sp setb it1 ;set int1 interrupt type (1:falling 0:low level) setb ex1 ;enable int1 interrupt setb ea ;open global interrupt switch loop: setb int1 ;ready read int1 port jnb int1,$ ;check int1 nop nop mov pcon,#02h ;mcu power down nop nop cpl p1.0 sjmp loop ;----------------------------------------- ;external interrupt1 service routine exint1: reti ;----------------------------------------- end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 168 www.stcmcu.com 6.7.3 programs of p3.4/t0/ int interrupt(falling edge) used to wake up pd mode 1. c program /*-----------------------------------------------------------------------------------------------------------------------------*/ /* --- stc mcu international limited --------------------------------------------------------------------------------*/ /* --- stc 1t series mcu power-down wakeup by t0 demo ----------------------------------------------------*/ /* ---this interrupt will borrow timer 0 interrupt request bit tf0 and timer 0 interrupt vector address ----*/ /* ---so timer 0 function should be disabled when this interrupt is enabled -------------------------------------*/ /* ---the enable bit of this interrupt is t0_pin_ie / wake_clko.4 in wake_clko register -----------*/ /* --- mobile: (86)13922809991 ----------------------------------------------------------------------------------------*/ /* --- fax: 86-755-82905966 --------------------------------------------------------------------------------------------*/ /* --- tel: 86-755-82948412 ---------------------------------------------------------------------------------------------*/ /* --- web: www.stcmcu.com ---------------------------------------------------------------------------------------*/ /* if you want to use the program or the program referenced in the ----------------------------------------------*/ /* article, please specify in which data and procedures from stc ----------------------------------------------*/ /*-----------------------------------------------------------------------------------------------------------------------------*/ #include "reg51.h" #include "intrins.h" sfr wake_clko = 0x8f; //external interrupt0 service routine void t0int( ) interrupt 1 //t0 interrupt, interrupt 1 (location at 000bh) { } void main() { wake_clko = 0x10; //enable p3.4/t0/ p3.4/t0/ int falling edge wakeup mcu //from power-down mode //t0_pin_ie (wake_clko.4) = 1 //et0 = 1; //enable t0 interrupt ea = 1; //open global interrupt switch while (1) { t0 = 1; //ready read t0 port while (!t0); //check t0 _nop_(); _nop_(); pcon = 0x02; //mcu power down _nop_(); _nop_(); p1++; } } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 169 www.stcmcu.com 2. assembly program /* --- stc mcu international limited --------------------------------------------------------------------------------*/ /* --- stc 1t series mcu power-down wakeup by t0 demo ----------------------------------------------------*/ /* ---this interrupt will borrow timer 0 interrupt request bit tf0 and timer 0 interrupt vector address ----*/ /* ---so timer 0 function should be disabled when this interrupt is enabled -------------------------------------*/ /* ---the enable bit of this interrupt is t0_pin_ie / wake_clko.4 in wake_clko register -----------*/ /* --- mobile: (86)13922809991 ----------------------------------------------------------------------------------------*/ /* --- fax: 86-755-82905966 --------------------------------------------------------------------------------------------*/ /* --- tel: 86-755-82948412 ---------------------------------------------------------------------------------------------*/ /* --- web: www.stcmcu.com ---------------------------------------------------------------------------------------*/ /* if you want to use the program or the program referenced in the ----------------------------------------------*/ /* article, please specify in which data and procedures from stc ----------------------------------------------*/ /*-----------------------------------------------------------------------------------------------------------------------------*/ wake_clko equ 8fh ;----------------------------------------- ;interrupt vector table org 0000h ljmp main org 000bh ;t0 interrupt, interrupt 1 (location at 000bh) ljmp t0int ;----------------------------------------- org 0100h main: mov sp,#7fh ;initial sp mov wake_clko, #10h ;enable p3.4/t0/ p3.4/t0/ int falling edge wakeup mcu ;from power-down mode ;t0_pin_ie (wake_clko.4) = 1 ; setb et0 ;enable t0 interrupt setb ea ;open global interrupt switch loop: setb t0 ;ready read t0 port jnb t0 ,$ ;check t0 nop nop mov pcon, #02h ;mcu power down nop nop cpl p1.0 sjmp loop ;----------------------------------------- ;t0 interrupt service routine t0int: reti ;----------------------------------------- end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 170 www.stcmcu.com 6.7.4 programs of p3.5/t1/ int interrupt(falling edge) used to wake up pd mode 1. c program /*-----------------------------------------------------------------------------------------------------------------------------*/ /* --- stc mcu international limited --------------------------------------------------------------------------------*/ /* --- stc 1t series mcu power-down wakeup by t1 demo ----------------------------------------------------*/ /* ---this interrupt will borrow timer 1 interrupt request bit tf1 and timer 1 interrupt vector address ----*/ /* ---so timer 1 function should be disabled when this interrupt is enabled -------------------------------------*/ /* ---the enable bit of this interrupt is t1_pin_ie / wake_clko.5 in wake_clko register -----------*/ /* --- mobile: (86)13922809991 ----------------------------------------------------------------------------------------*/ /* --- fax: 86-755-82905966 --------------------------------------------------------------------------------------------*/ /* --- tel: 86-755-82948412 ---------------------------------------------------------------------------------------------*/ /* --- web: www.stcmcu.com ---------------------------------------------------------------------------------------*/ /* if you want to use the program or the program referenced in the ----------------------------------------------*/ /* article, please specify in which data and procedures from stc ----------------------------------------------*/ /*-----------------------------------------------------------------------------------------------------------------------------*/ #include "reg51.h" #include "intrins.h" sfr wake_clko = 0x8f; //external interrupt0 service routine void t1int() interrupt 3 //t1 interrupt, interrupt 3 (location at 001bh) { } void main() { wake_clko = 0x20; //enable p3.5/t1/ p3.5/t1/ int falling edge wakeup mcu //from power-down mode //t1_pin_ie / wake_clko.5 = 1 //et1 = 1; //enable t1 interrupt ea = 1; //open global interrupt switch while (1) { t1 = 1; //ready read t1 port while (!t1); //check t1 _nop_(); _nop_(); pcon = 0x02; //mcu power down _nop_(); _nop_(); p1++; } } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 171 www.stcmcu.com 2. assembly program /* --- stc mcu international limited --------------------------------------------------------------------------------*/ /* --- stc 1t series mcu power-down wakeup by t1 demo ----------------------------------------------------*/ /* ---this interrupt will borrow timer 1 interrupt request bit tf1 and timer 1 interrupt vector address ----*/ /* ---so timer 1 function should be disabled when this interrupt is enabled -------------------------------------*/ /* ---the enable bit of this interrupt is t1_pin_ie / wake_clko.5 in wake_clko register -----------*/ /* --- mobile: (86)13922809991 ----------------------------------------------------------------------------------------*/ /* --- fax: 86-755-82905966 --------------------------------------------------------------------------------------------*/ /* --- tel: 86-755-82948412 ---------------------------------------------------------------------------------------------*/ /* --- web: www.stcmcu.com ---------------------------------------------------------------------------------------*/ /* if you want to use the program or the program referenced in the ----------------------------------------------*/ /* article, please specify in which data and procedures from stc ----------------------------------------------*/ /*-----------------------------------------------------------------------------------------------------------------------------*/ wake_clko equ 8fh ;----------------------------------------- ;interrupt vector table org 0000h ljmp main org 001bh ;t1 interrupt, interrupt 3 (location at 001bh) ljmp t1int ;----------------------------------------- org 0100h main: mov sp, #7fh ;initial sp mov wake_clko, #20h ;enable p3.5/t1/ p3.5/t1/ int falling edge wakeup mcu ;from power-down mode ;t1_pin_ie / wake_clko.5 = 1 ;setb et1 ;enable t1 interrupt setb ea ;open global interrupt switch loop: setb t1 ;ready read t1 port jnb t1, $ ;check t1 nop nop mov pcon, #02h ;mcu power down nop nop cpl p1.0 sjmp loop ;----------------------------------------- ;t1 interrupt service routine t1int: reti ;----------------------------------------- end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 172 www.stcmcu.com 6.7.5 program of p3.0/rxd/ int interrupt(falling edge) used to wake up pd mode 1. c program /*-----------------------------------------------------------------------------------------------------------------------------*/ /* --- stc mcu international limited --------------------------------------------------------------------------------*/ /* --- stc 1t series mcu power-down wakeup by rxd demo -------------------------------------------------*/ /* ---this interrupt will borrow rxd interrupt request bit ri and its interrupt vector address ----------------*/ /* ---so uart function should be disabled when this interrupt is enabled --------------------------------------*/ /* ---the enable bit of this interrupt is rxd_pin_ie / wake_clko.6 in wake_clko register --------*/ /* --- mobile: (86)13922809991 ----------------------------------------------------------------------------------------*/ /* --- fax: 86-755-82905966 --------------------------------------------------------------------------------------------*/ /* --- tel: 86-755-82948412 ---------------------------------------------------------------------------------------------*/ /* --- web: www.stcmcu.com ---------------------------------------------------------------------------------------*/ /* if you want to use the program or the program referenced in the ----------------------------------------------*/ /* article, please specify in which data and procedures from stc ----------------------------------------------*/ /*-----------------------------------------------------------------------------------------------------------------------------*/ #include "reg51.h" #include "intrins.h" typedef unsigned char byte; typedef unsigned int word; /*declare sfr associated with the rxd */ sfr wake_clko = 0x8f; void uart_isr() interrupt 4 using 1 { if (ri) { ri = 0; } } void main() { wake_clko = 0x40; //enable p3.0/rxd/ int falling edge wakeup mcu //from power-down mode //rxd_pin_ie (wake_clko.6) = 1 es = 1; ea = 1; stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 173 www.stcmcu.com while (1) { rxd = 1; //ready read rxd port while (!rxd); //check rxd _nop_(); _nop_(); pcon = 0x02; //mcu power down _nop_(); _nop_(); p2++; } } 2. assembly program /*-----------------------------------------------------------------------------------------------------------------------------*/ /* --- stc mcu international limited --------------------------------------------------------------------------------*/ /* --- stc 1t series mcu power-down wakeup by rxd demo -------------------------------------------------*/ /* ---this interrupt will borrow rxd interrupt request bit ri and its interrupt vector address ----------------*/ /* ---so uart function should be disabled when this interrupt is enabled --------------------------------------*/ /* ---the enable bit of this interrupt is rxd_pin_ie / wake_clko.6 in wake_clko register --------*/ /* --- mobile: (86)13922809991 ----------------------------------------------------------------------------------------*/ /* --- fax: 86-755-82905966 --------------------------------------------------------------------------------------------*/ /* --- tel: 86-755-82948412 ---------------------------------------------------------------------------------------------*/ /* --- web: www.stcmcu.com ---------------------------------------------------------------------------------------*/ /* if you want to use the program or the program referenced in the ----------------------------------------------*/ /* article, please specify in which data and procedures from stc ----------------------------------------------*/ /*-----------------------------------------------------------------------------------------------------------------------------*/ ;/*declare sfr associated with the rxd */ wake_clko equ 8fh ;----------------------------------------- org 0000h ljmp main org 0023h uart_isr: jbc ri, exit ;clear ri flag exit: reti ;----------------------------------------- stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 174 www.stcmcu.com org 0100h main: mov wake_clko, #40h ;enable p3.0/rxd/ int falling edge wakeup mcu ;from power-down mode ;rxd_pin_ie (wake_clko.6) = 1 setb es setb ea loop: setb rxd ;ready read rxd port jnb rxd, $ ;check rxd nop nop mov pcon, #02h ;mcu power down nop nop cpl p1.0 sjmp loop ;----------------------------------------- end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 175 www.stcmcu.com 6.7.6 demo program of low voltage detection interrupt used to wake up pd mode 1. c program /*----------------------------------------------------------------------------------------------*/ /* --- stc mcu international limited -------------------------------------------------*/ /* --- stc 1t series mcu power-down wakeup by lvd (p4.6) demo ----------*/ /* --- mobile: (86)13922809991 ---------------------------------------------------------*/ /* --- fax: 86-755-82905966 -------------------------------------------------------------*/ /* --- tel: 86-755-82948412 --------------------------------------------------------------*/ /* --- web: www.stcmcu.com ---------------------------------------------------------*/ /* if you want to use the program or the program referenced in the ----------------*/ /* article, please specify in which data and procedures from stc ----------------*/ /*-----------------------------------------------------------------------------------------------*/ #include "reg51.h" #include "intrins.h" sfr wake_clko = 0x8f; sfr p4sw = 0xbb; sbit elvd = ie^6; //external interrupt0 service routine void lvdint() interrupt 6 //lvd interrupt, interrupt 6 (location at 0033h) { pcon &= 0xdf; //clear lvd flag } void main() { p4sw &= 0xbf; //set p4.6 as lvd function pin wake_clko = 0x08; //enable lvd signal wakeup mcu from power-down mode elvd = 1; //enable lvd interrupt ea = 1; //open global interrupt switch while (1) { while (pcon & 0x20) { pcon &= 0xdf; //clear lvd flag _nop_(); _nop_(); _nop_(); _nop_(); } _nop_(); _nop_(); pcon = 0x02; //mcu power down _nop_(); _nop_(); p1++; } } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 176 www.stcmcu.com 2. assembly program /*----------------------------------------------------------------------------------------------*/ /* --- stc mcu international limited -------------------------------------------------*/ /* --- stc 1t series mcu power-down wakeup by lvd (p4.6) demo ----------*/ /* --- mobile: (86)13922809991 ---------------------------------------------------------*/ /* --- fax: 86-755-82905966 -------------------------------------------------------------*/ /* --- tel: 86-755-82948412 --------------------------------------------------------------*/ /* --- web: www.stcmcu.com ---------------------------------------------------------*/ /* if you want to use the program or the program referenced in the ----------------*/ /* article, please specify in which data and procedures from stc ----------------*/ /*-----------------------------------------------------------------------------------------------*/ wake_clko equ 8fh p4sw equ 0bbh elvd bit ie.6 ;----------------------------------------- ;interrupt vector table org 0000h ljmp main org 0033h ;lvd interrupt, interrupt 6 (location at 0033h) ljmp lvdint ;----------------------------------------- org 0100h main: mov sp, #7fh ;initial sp anl p4sw, #0bfh ;set p4.6 as lvd function pin mov wake_clko,#08h ;enable lvd signal wakeup mcu from power-down mode setb elvd ;enable lvd interrupt setb ea ;open global interrupt switch loop: anl pcon, #0dfh ;clear lvd flag nop nop nop nop mov a, pcon ;check lvd flag jb acc.5, loop stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 177 www.stcmcu.com nop nop mov pcon, #02h ;mcu power down nop nop cpl p1.0 sjmp loop ;----------------------------------------- ;t1 interrupt service routine lvdint: anl pcon, #0dfh ;clear lvd flag reti ;----------------------------------------- end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 178 www.stcmcu.com 6.7.7 program of pca interrupt used to wake up power down mode 1. c program /*--------------------------------------------------------------------------------------------*/ /* --- stc mcu international limited -----------------------------------------------*/ /* --- stc 1t series mcu power-down wakeup by pca demo ----------------*/ /* --- mobile: (86)13922809991 -------------------------------------------------------*/ /* --- fax: 86-755-82905966 -----------------------------------------------------------*/ /* --- tel: 86-755-82948412 ------------------------------------------------------------*/ /* --- web: www.stcmcu.com ------------------------------------------------------*/ /* if you want to use the program or the program referenced in the -------------*/ /* article, please specify in which data and procedures from stc -------------*/ /*--------------------------------------------------------------------------------------------*/ #include "reg51.h" #include "intrins.h" typedef unsigned char byte; typedef unsigned int word; /*declare sfr associated with the pca */ sfr wake_clko = 0x8f; sfr ccon = 0xd8; //pca control register sbit ccf0 = ccon^0; //pca module-0 interrupt flag sbit ccf1 = ccon^1; //pca module-1 interrupt flag sbit cr = ccon^6; //pca timer run control bit sbit cf = ccon^7; //pca timer overflow flag sfr cmod = 0xd9; //pca mode register sfr cl = 0xe9; //pca base timer low sfr ch = 0xf9; //pca base timer high sfr ccapm0= 0xda; //pca module-0 mode register sfr ccap0l = 0xea; //pca module-0 capture register low sfr ccap0h = 0xfa; //pca module-0 capture register high sfr ccapm1= 0xdb; //pca module-1 mode register sfr ccap1l = 0xeb; //pca module-1 capture register low sfr ccap1h = 0xfb; //pca module-1 capture register high sfr ccapm2= 0xdc; //pca module-2 mode register sfr ccap2l = 0xec; //pca module-2 capture register low sfr ccap2h = 0xfc; //pca module-2 capture register high sfr ccapm3= 0xdd; //pca module-3 mode register sfr ccap3l = 0xed; //pca module-3 capture register low sfr ccap3h = 0xfd; //pca module-3 capture register high sfr pcapwm0 = 0xf2; sfr pcapwm1 = 0xf3; sfr pcapwm2 = 0xf4; sfr pcapwm3 = 0xf5; stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 179 www.stcmcu.com sbit pca_led = p1^0; //pca test led sbit ccp0 = p1^3; void pca_isr() interrupt 7 using 1 { ccf0 = 0; //clear interrupt flag pca_led = !pca_led; //toggle the test pin while ccp0(p1.3) have a falling edge } void main() { ccon = 0; //initial pca control register //pca timer stop running //clear cf flag //clear all module interrupt flag cl = 0; //reset pca base timer ch = 0; cmod = 0x00; //set pca timer clock source as fosc/12 //disable pca timer overflow interrupt ccapm0 = 0x11; //pca module-0 capture by a negative tigger on ccp0(p1.3) //and enable pca interrupt // ccapm0 = 0x21; //pca module-0 capture by a rising edge on ccp0(p1.3) //and enable pca interrupt // ccapm0 = 0x31; //pca module-0 capture by a transition (falling/rising edge) //on ccp0(p1.3) and enable pca interrupt wake_clko = 0x80; //enable pca falling/raising edge wakeup mcu from power-down mode cr = 1; //pca timer start run ea = 1; while (1) { ccp0 = 1; //ready read ccp0 port while (!ccp0); //check ccp0 _nop_(); _nop_(); pcon = 0x02; //mcu power down _nop_(); _nop_(); p2++; } } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 180 www.stcmcu.com 2 assembly program /*--------------------------------------------------------------------------------------------*/ /* --- stc mcu international limited -----------------------------------------------*/ /* --- stc 1t series mcu power-down wakeup by pca demo ----------------*/ /* --- mobile: (86)13922809991 -------------------------------------------------------*/ /* --- fax: 86-755-82905966 -----------------------------------------------------------*/ /* --- tel: 86-755-82948412 ------------------------------------------------------------*/ /* --- web: www.stcmcu.com ------------------------------------------------------*/ /* if you want to use the program or the program referenced in the -------------*/ /* article, please specify in which data and procedures from stc -------------*/ /*--------------------------------------------------------------------------------------------*/ ;/*declare sfr associated with the pca */ wake_clko equ 8fh ccon equ 0d8h ;pca control register ccf0 bit ccon.0 ;pca module-0 interrupt flag ccf1 bit ccon.1 ;pca module-1 interrupt flag cr bit ccon.6 ;pca timer run control bit cf bit ccon.7 ;pca timer overflow flag cmod equ 0d9h ;pca mode register cl equ 0e9h ;pca base timer low ch equ 0f9h ;pca base timer high ccapm0 equ 0dah ;pca module-0 mode register ccap0l equ 0eah ;pca module-0 capture register low ccap0h equ 0fah ;pca module-0 capture register high ccapm1 equ 0dbh ;pca module-1 mode register ccap1l equ 0ebh ;pca module-1 capture register low ccap1h equ 0fbh ;pca module-1 capture register high ccapm2 equ 0dch ;pca module-2 mode register ccap2l equ 0ech ;pca module-2 capture register low ccap2h equ 0fch ;pca module-2 capture register high ccapm3 equ 0ddh ;pca module-3 mode register ccap3l equ 0edh ;pca module-3 capture register low ccap3h equ 0fdh ;pca module-3 capture register high pca_led bit p1.1 ;pca test led ccp0 bit p1.3 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 181 www.stcmcu.com ;----------------------------------------- org 0000h ljmp main org 003bh pca_isr: clr ccf0 ;clear interrupt flag cpl pca_led ;toggle the test pin while ccp0(p1.3) have a falling edge reti ;----------------------------------------- org 0100h main: mov ccon, #0 ;initial pca control register ;pca timer stop running ;clear cf flag ;clear all module interrupt flag clr a ; mov cl, a ;reset pca base timer mov ch, a ; mov cmod, #00h ;set pca timer clock source as fosc/12 ;disable pca timer overflow interrupt mov ccapm0, #11h ;pca module-0 capture by a falling edge on ccp0(p1.3) ;and enable pca interrupt ; mov ccapm0, #21h ;pca module-0 capture by a rising edge on ccp0(p1.3) ;and enable pca interrupt ; mov ccapm0, #31h ;pca module-0 capture by a transition (falling/rising edge) ;on ccp0(p1.3) and enable pca interrupt ;------------------------------- mov wake_clko, #80h ;enable pca falling/raising edge wakeup mcu from ;power-down mode setb cr ;pca timer start run setb ea loop: setb ccp0 ;ready read ccp0 port jnb ccp0, $ ;check ccp0 nop nop mov pcon, #02h ;mcu power down nop nop cpl p1.0 sjmp loop ;----------------------------------------- end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 182 www.stcmcu.com chapter 7. timer/counter 0/1 timer 0 and timer 1 are like the ones in the conventional 8051, both of them can be individually configured as timers or event counters. in the timer function, the register is incremented every 12 system clocks or every system clock depending on auxr.7(t0x12) bit and auxr.6(t1x12). in the default state, it is fully the same as the conventional 8051. in the x12 mode, the count rate equals to the system clock. in the counter function, the register is incremented in response to a 1-to-0 transition at its corresponding external input pin, t0 or t1. in this function, the external input is sampled once at the positive edge of every clock cycle. when the samples show a high in one cycle and a low in the next cycle, the count is incremented. the new count value appears in the register during at the end of the cycle following the one in which the transition was detected. since it takes 2 machine cycles (24 system clocks) to recognize a l-to-0 transition, the maximum count rate is 1/24 of the system clock. there are no restrictions on the duty cycle of the external input signal, but to ensure that a given level is sampled at least once before it changes, it should be held for at least one full machine cycle. in addition to the timer or counter selection, timer 0 and timer 1 have four operating modes from which to select. the timer or counter function is selected by control bits c/t in the special function register tmod. these two timer/counter have four operating modes, which are selected by bit-pairs (m1, m0) in tmod. modes 0, 1, and 2 are the same for both timer/counters. mode 3 is different.the four operating modes are described in the following text. symbol description address bit address and symbol msb lsb value after power-on or reset tcon timer control 88h tf1 tr1 tf0 tr0 ie1 it1 ie0 it0 0000 0000b tmod timer mode 89h gate c/t m1 m0 gate c/t m1 m0 0000 0000b tl0 timer low 0 8ah 0000 0000b tl1 timer low 1 8bh 0000 0000b th0 timer high 0 8ch 0000 0000b th1 timer high 1 8dh 0000 0000b auxr auxiliary register 8eh t0x12 t1x12 uart_m0x6 brtr s2smod brtx12 extram s1brs 0000 0000b wake_clko clk_output power down wake-up control register 8fh pcawakeup rxd_pin_ie t1_pin_ie t0_pin_ie lvd_wake brtclko t1clko t0clko 0000 0000b 7.1 special function registers about timer/counter stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 183 www.stcmcu.com 1. tcon register : timer/counter control register (bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 tcon 88h name tf1 tr1 tf0 tr0 ie1 it1 ie0 it0 tf1: timer/counter 1 overflow flag. set by hardware on timer/counter 1 overflow. the flag can be cleared by software but is automatically cleared by hardware when processor vectors to the timer 1 interrupt routine. if tf1 = 0, no timer 1 overflow detected. if tf1 = 1, timer 1 has overflowed. tr1: timer/counter 1 run control bit . set/cleared by software to turn timer/counter on/off. if tr1 = 0, timer 1 disabled. if tr1 = 1, timer 1 enabled. tf0: timer/counter 0 overflow flag. set by hardware on timer/counter 0 overflow. the flag can be cleared by software but is automatically cleared by hardware when processor vectors to the timer 0 interrupt routine. if tf0 = 0, no timer 0 overflow detected. if tf0 = 1, timer 0 has overflowed. tr0: timer/counter 0 run control bit . set/cleared by software to turn timer/counter on/off. if tr0 = 0, timer 0 disabled. if tr0 = 1, timer 0 enabled. ie1: external interrupt 1 edge flag. set by hardware when external interrupt edge/level defined by it1 is detected. the flag can be cleared by software but is automatically cleared when the external interrupt 1 service routine has been processed. it1: external intenupt 1 type select bit. set/cleared by software to specify falling edge/low level triggered ex - ternal interrupt 1. if it1 = 0, int1 is low level triggered. if it1 = 1, int1 is edge triggered. ie0: external interrupt 0 edge flag. set by hardware when external interrupt edge/level defined by it0 is detected. the flag can be cleared by software but is automatically cleared when the external interrupt 0 service routine has been processed. it0: external intenupt 0 type select bit. set/cleared by software to specify falling edge/low level triggered ex - ternal interrupt 0. if it0 = 0, int0 is low level triggered. if it0 = 1, int0 is edge triggered. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 184 www.stcmcu.com 2. tmod register : timer/counter mode register tmod address: 89h (non bit-addressable) gate c/t m1 m0 gate c/t m1 m0 (msb) (lsb) } } timer 1 timer 0 gatr/tmod.7: timer/counter gate control. if gate/tmod.7=0,timer/counter 1 enabled when tr1 is set irrespective of int1 logic level; if gate/tmod.7=1, timer/counter 1 enabled only when tr1 is set and int1 pin is high. c/t /tmod.6: timer/counter 1 select bit. if c/t /tmod.6=0,timer/counter 1 is set for timer operation (input from internal system clock); if c/t /tmod.6=0,timer/counter 1 is set for counter operation (input from external t1 pin). m1/tmod.5 ~ m0/tmod.4: timer 1 mode select bits. m1 m0 operating mode 0 0 mode 0: 13-bit timer/counter for timer 1 0 1 mode 1: 16-bit timer/counter. th1and tl1 are cascaded; there is no prescaler. 1 0 mode 2: 8-bit auto-reload timer/counter. th1 holds a value which is to be reloaded into tl1 each time it overflows. 1 1 timer/counter 1 stopped gatr/tmod.3: timer/counter gate control. if gate/tmod.3=0,timer/counter 0 enabled when tr0 is set irrespective of int0 logic level; if gate/tmod.3=1, timer/counter 0 enabled only when tr0 is set and int0 pin is high. c/t /tmod.2: timer/counter 0 select bit. if c/t /tmod.2=0,timer/counter 0 is set for timer operation (input from internal system clock); if c/t /tmod.2=0,timer/counter 0 is set for counter operation (input from external t0 pin). m1/tmod.1 ~ m0/tmod.0: timer 0 mode select bits. m1 m0 operating mode 0 0 mode 0: 13-bit timer/counter for timer 0 0 1 mode 1: 16-bit timer/counter. th0 and tl0 are cascaded; there is no prescaler. 1 0 mode 2: 8-bit auto-reload timer/counter. th0 holds a value which is to be reloaded into tl0 each time it overflows. 1 1 mode3: tl0 is an 8-bit timer/counter controlled by the standard timer 0 control bits th0 is an 8-bit timer only controlled by timer 1 control bits. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 185 www.stcmcu.com 3. auxr: auxiliary register (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 auxr 8eh name t0x12 t1x12 uart_m0x6 brtr s2smod brtx12 extram s1brs t0x12 : timer 0 clock source bit. 0 : the clock source of timer 0 is sysclk/12. it will compatible to the traditional 80c51 mcu 1 : the clock source of timer 0 is sysclk/1. it will drive the t0 faster than a traditional 80c51 mcu t1x12 : timer 1 clock source bit. 0 : the clock source of timer 1 is sysclk/12. it will compatible to the traditional 80c51 mcu 1 : the clock source of timer 1 is sysclk/1. it will drive the t0 faster than a traditional 80c51 mcu uart_m0x6 : baud rate select bit of uart1 while it is working under mode-0 0 : the baud-rate of uart in mode 0 is sysclk/12. 1 : the baud-rate of uart in mode 0 is sysclk/2. brtr : dedicated baud-rate timer run control bit. 0 : the baud-rate generator is stopped. 1 : the baud-rate generator is enabled. s2smod : the baud-rate of uart2 double contol bit. 0 : default. the baud-rate of uart2 (s2) is not doubled. 1 : the baud-rate uart2 (s2) is doubled. brtx12 : dedicated baud-rate timer counter control bit. 0 : the baud-rate generator is incremented every 12 system clocks. 1 : the baud-rate generator is incremented every system clock. extram : internal / external ram access control bit. 0 : on-chip auxiliary ram is enabled and located at the address 0x0000 to 0x03ff. for address over 0x03ff, off-chip expanded ram becomes the target automatically. 1 : on-chip auxiliary ram is always disabled. s1brs : the baud-rate generator of uart1 select bit. 0 : default. select timer 1 as the baud-rate generator of uart1 1 : timer 1 is replaced by the independent baud-rate generator which is selected as the baud-rate of uart. in other words, timer 1 is released to use in other functions. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 186 www.stcmcu.com 4. wake_clko: clk_output power down wake-up control register (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 wake_clko 8fh name pcawakeup rxd_pin_ie t1_pin_ie t0_pin_ie lvd_wake brtclko t1clko t0clko pcawakeup: when set and the associated-pca interrupt control registers is configured correctly, the cexn pin of pca function is enabled to wake up mcu from power-down state. rxd_pin_ie: when set and the associated-uart interrupt control registers is configured correctly, the rxd pin (p3.0) is enabled to wake up mcu from power-down state. t1_pin_ie : when set and the associated-timer1 interrupt control registers is configured correctly, the t1 pin (p3.5) is enabled to wake up mcu from power-down state. t0_pin_ie : when set and the associated-timer0 interrupt control registers is configured correctly, the t1 pin (p3.4) is enabled to wake up mcu from power-down state. lvd_wake: when set and the associated-lvd interrupt control registers is configured correctly, the cmpin pin is enabled to wake up mcu from power-down state. brtcklo : when set, p1.0 is enabled to be the clock output of baud-rate timer (brt). the clock rate is brg overflow rate divided by 2. t1cklo : when set, p3.5 is enabled to be the clock output of timer 1. the clock rate is timer 1overflow rate divided by 2. t0cklo : when set, p3.4 is enabled to be the clock output of timer 0. the clock rate is timer 0overflow rate divided by 2. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 187 www.stcmcu.com mode 0 in this mode, the timer 0 is configured as a 13-bit timer/counter. as the count rolls over from all 1s to all 0s, it sets the timer interrupt flag tf0. the counted input is enabled to the timer when tr0 = 1 and either gate=0 or int0 = 1.(setting gate = 1 allows the timer to be controlled by external input int0 , to facilitate pulse width mea - surements.) tr0 is a control bit in the special function register tcon. gate is in tmod. the 13-bit register consists of all 8 bits of th0 and the lower 5 bits of tl0. the upper 3 bits of tl0 are indeterminate and should be ignored. setting the run flag (tr0) does not clear the registers. there are two different gate bits. one for timer 1 (tmod.7) and one for timer 0 (tmod.3). 7.2 timer/counter 0 mode of operation (compatible with traditional 8051 mcu) timer/counter 0 mode 0: 13-bit timer/counter interrupt sysclk tl0 (5 bits) th0 (8 bits) tf0 control c/t=0 c/t=1 t0 pin tr0 gate int0 auxr.7/t0x12=0 12 1 auxr.7/t0x12=1 7.2.1 mode 0 (13-bit timer/counter) timer/counter 0 can be configured for four modes by setting m1(tmod.1) and m0(tmod.0) in sepcial function register tmod. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 188 www.stcmcu.com in this mode, the timer register is configured as a 16-bit register. as the count rolls over from all 1s to all 0s, it sets the timer interrupt flag tf0. the counted input is enabled to the timer when tr0 = 1 and either gate=0 or int0 = 1.(setting gate = 1 allows the timer to be controlled by external input int0 , to facilitate pulse width measurements.) tr0 is a control bit in the special function register tcon. gate is in tmod. the 16-bit register consists of all 8 bits of th0 and the lower 8 bits of tl0. setting the run flag (tr0) does not clear the registers. mode 1 is the same as mode 0, except that the timer register is being run with all 16 bits. timer/counter 0 mode 1 : 16-bit timer/counter interrupt sysclk tl0 (8 bits) th0 (8 bits) tf0 control c/t=0 c/t=1 t0 pin tr0 gate int0 auxr.7/t0x12=0 12 1 auxr.7/t0x12=1 c program: /* -------------------------------------------------------------------------------*/ /* --- stc mcu international limited ----------------------------------*/ /* --- stc 1t series 16-bit timer demo --------------------------------*/ /* --- mobile: (86)13922809991 ------------------------------------------*/ /* --- fax: 86-755-82905966 ----------------------------------------------*/ /* --- tel: 86-755-82948412 -----------------------------------------------*/ /* --- web: www.stcmcu.com ------------------------------------------*/ /* if you want to use the program or the program referenced in the */ /* article, please specify in which data and procedures from stc */ /*-------------------------------------------------------------------------------*/ #include "reg51.h" typedef unsigned char byte; typedef unsigned int word; //----------------------------------------------- /* define constants */ #define fosc 18432000l #define mode1t //timer clock mode, comment this line is 12t mode, uncomment is 1t mode there are two simple programs that demostrates timer 0 as 16-bit timer/counter, one written in c language while other in assembly language. 7.2.2 mode 1 (16-bit timer/counter) and demo programs (c and asm) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 189 www.stcmcu.com #ifdef mode1t #define t1ms (65536-fosc/1000) //1ms timer calculation method in 1t mode #else #define t1ms (65536-fosc/12/1000) //1ms timer calculation method in 12t mode #endif /* define sfr */ sfr auxr = 0x8e; //auxiliary register sbit test_led = p0^0; //work led, flash once per second /* define variables */ word count; //1000 times counter //----------------------------------------------- /* timer0 interrupt routine */ void tm0_isr() interrupt 1 using 1 { tl0 = t1ms; //reload timer0 low byte th0 = t1ms >> 8; //reload timer0 high byte if (count-- == 0) //1ms * 1000 -> 1s { count = 1000; //reset counter test_led = ! test_led; //work led flash } } //----------------------------------------------- /* main program */ void main() { #ifdef mode1t auxr = 0x80; //timer0 work in 1t mode #endif tmod = 0x01; //set timer0 as mode1 (16-bit) tl0 = t1ms; //initial timer0 low byte th0 = t1ms >> 8; //initial timer0 high byte tr0 = 1; //timer0 start running et0 = 1; //enable timer0 interrupt ea = 1; //open global interrupt switch count = 0; //initial counter while (1); //loop } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 190 www.stcmcu.com assembly program: ;/*-------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited ----------------------------------*/ ;/* --- stc 1t series 16-bit timer demo --------------------------------*/ ;/* --- mobile: (86)13922809991 -------------- ---------------------------*/ ;/* --- fax: 86-755-82905966 ----------------------------------------------*/ ;/* --- tel: 86-755-82948412 -----------------------------------------------*/ ;/* --- web: www.stcmcu.com -----------------------------------------*/ ;/* if you want to use the program or the program referenced in the */ ;/* article, please specify in which data and procedures from stc */ ;/*------------------------------------------------------------------------------*/ ;/* define constants */ #define mode1t ;timer clock mode, comment this line is 12t mode, uncomment is 1t mode #ifdef mode1t t1ms equ 0b800h ;1ms timer calculation method in 1t mode is (65536-18432000/1000) #else t1ms equ 0fa00h ;1ms timer calculation method in 12t mode is (65536-18432000/12/1000) #endif ;/* define sfr */ auxr data 8eh ;auxiliary register test_led bit p1.0 ;work led, flash once per second ;/* define variables */ count data 20h ;1000 times counter (2 bytes) ;----------------------------------------------- org 0000h ljmp main org 000bh ljmp tm0_isr ;----------------------------------------------- ;/* main program */ main: #ifdef mode1t mov auxr, #80h ;timer0 work in 1t mode #endif mov tmod, #01h ;set timer0 as mode1 (16-bit) mov tl0, #low t1ms ;initial timer0 low byte mov th0, #high t1ms ;initial timer0 high byte setb tr0 ;timer0 start running setb et0 ;enable timer0 interrupt setb ea ;open global interrupt switch clr a mov count, a mov count+1, a ;initial counter sjmp $ stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 191 www.stcmcu.com ;----------------------------------------------- ;/* timer0 interrupt routine */ tm0_isr: push acc push psw mov tl0, #low t1ms ;reload timer0 low byte mov th0, #high t1ms ;reload timer0 high byte mov a, count orl a, count+1 ;check whether count(2byte) is equal to 0 jnz skip mov count, #low 1000 ;1ms * 1000 -> 1s mov count+1,#high 1000 cpl test_led ;work led flash skip: clr c mov a, count ;count-- subb a, #1 mov count, a mov a, count+1 subb a, #0 mov count+1,a pop psw pop acc reti ;----------------------------------------------- end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 192 www.stcmcu.com timer/counter 0 mode 2: 8-bit auto-reload sysclk control c/t=0 c/t=1 t0 pin tr0 gate int0 auxr.7/t0x12=0 auxr.7/t0x12=1 tl0 (8 bits) th0 (8 bits) 12 1 interrupt tf0 toggle t0clko p3.4 clkout0 mode 2 configures the timer register as an 8-bit counter(tl0) with automatic reload. overflow from tl0 not only set tf0, but also reload tl0 with the content of th0, which is preset by software. the reload leaves th0 unchanged. stc12c5a60s2 is able to generate a programmable clock output on p3.4. when t0clko/ wake_clko.0 bit in wake_clko sfr is set, t0 timer overflow pulse will toggle p3.4 latch to generate a 50% duty clock. the frequency of clock-out = t0 overflow rate /2. if c/t (tmod.2) = 0, timer/counter 0 is set for timer operation (input from internal system clock), the frequency of clock-out is as following : (sysclk) / (256 C th0) / 2, when auxr.7 / t0x12=1 or (sysclk / 12) / (256 C th0) / 2 , when auxr.7 / t0x12=0 if c/t (tmod.2) = 1, timer/counter 0 is set for conter operation (input from external p3.4/t0 pin), the frequency of clock-out is as following : t0_pin_clk / (256-th0) / 2 7.2.3 mode 2 (8-bit auto-reload mode) and demo programs (c and assembly program) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 193 www.stcmcu.com ;t0 interrupt (falling edge) demo programs, where t0 operated in mode 2 (8-bit auto-relaod mode) ; the timer interrupt can not wake up mcu from power-down mode in the following programs 1. c program /*----------------------------------------------------------------------------------*/ /* --- stc mcu international limited -------------------------------------*/ /* --- stc 1t series mcu t0 (falling edge) demo ---------------------*/ /* --- mobile: (86)13922809991 --------------------------------------------*/ /* --- fax: 86-755-82905966 ------------------------------------------------*/ /* --- tel: 86-755-82948412 -------------------------------------------------*/ /* --- web: www.stcmcu.com -------------------------------------------*/ /* if you want to use the program or the program referenced in the --*/ /* article, please specify in which data and procedures from stc ---*/ /*---------------------------------------------------------------------------------*/ #include "reg51.h" sfr auxr = 0x8e; //auxiliary register //t0 interrupt service routine void t0int( ) interrupt 1 //t0 interrupt (location at 000bh) { } void main() { auxr = 0x80; //timer0 work in 1t mode tmod = 0x06; //set timer0 as counter mode2 (8-bit auto-reload) tl0 = th0 = 0xff; //fill with 0xff to count one time tr0 = 1; //timer0 start run et0 = 1; //enable t0 interrupt ea = 1; //open global interrupt switch while (1); } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 194 www.stcmcu.com 2. assembly program /*-------------------------------------------------------------------------------*/ /* --- stc mcu international limited ----------------------------------*/ /* --- stc 1t series mcu t0(falling edge) demo -------------------*/ /* --- mobile: (86)13922809991 ------------------------------------------*/ /* --- fax: 86-755-82905966 ----------------------------------------------*/ /* --- tel: 86-755-82948412 -----------------------------------------------*/ /* --- web: www.stcmcu.com -----------------------------------------*/ /* if you want to use the program or the program referenced in the */ /* article, please specify in which data and procedures from stc */ /*------------------------------------------------------------------------------*/ auxr data 08eh ;auxiliary register ;----------------------------------------- ;interrupt vector table org 0000h ljmp main org 000bh ;t0 interrupt (location at 000bh) ljmp t0int ;----------------------------------------- org 0100h main: mov sp, #7fh ;initial sp mov auxr, #80h ;timer0 work in 1t mode mov tmod, #06h ;set timer0 as counter mode2 (8-bit auto-reload) mov a, #0ffh mov tl0, a ;fill with 0xff to count one time mov th0, a setb tr0 ;timer0 start run setb et0 ;enable t0 interrupt setb ea ;open global interrupt switch sjmp $ ;----------------------------------------- ;t0 interrupt service routine t0int: reti ;----------------------------------------- end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 195 www.stcmcu.com timer 1 in mode 3 simply holds its count, the effect is the same as setting tr1 = 0. timer 0 in mode 3 established tl0 and th0 as two separate 8-bit counters. tl0 use the timer 0 control bits: c/t ,gate,tr0, int0 and tf0. th0 is locked into a timer function (counting machine cycles) and takes over the use of tr1 from tmer 1. thus, th0 now controls the timer 1 interrupt. mode 3 is provided for applications requiring an extra 8-bit timer or counter. when timer 0 is in mode 3, timer 1 can be turned on and off by switching it out of and into its own mode 3, or can still be used by the serial port as a baud rate generator, or in fact, in any application not requiring an interrupt. timer/counter 0 mode 3: two 8-bit timers/counters interrupt sysclk tf0 control c/t=0 c/t=1 t0 pin tr0 gate int0 auxr.7/t0x12=0 auxr.7/t0x12=1 tl0 (8 bit) control th0 (8 bits) tf1 interrupt tr1 12 1 sysclk 12 1 auxr.7/t0x12=0 auxr.7/t0x12=0 7.2.4 mode 3 (two 8-bit timers/couters) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 196 www.stcmcu.com timer/counter 1 mode 0: 13-bit timer/counter interrupt sysclk tl1 (5 bits) th1 (8 bits) tf1 control c/t=0 c/t=1 t1 pin tr1 gate int1 auxr.6/t1x12=0 12 1 auxr.6/t1x12=1 7.3 timer/counter 1 mode of operation in this mode, the timer register is configured as a 13-bit register. as the count rolls over from all 1s to all 0s, it sets the timer interrupt flag tf1. the counted input is enabled to the timer when tr1 = 1 and either gate=0 or int1 = 1.(setting gate = 1 allows the timer to be controlled by external input int1 , to facilitate pulse width measurements.) tr0 is a control bit in the special function register tcon. gate is in tmod. the 13-bit register consists of all 8 bits of th1 and the lower 5 bits of tl1. the upper 3 bits of tl1 are indeterminate and should be ignored. setting the run flag (tr1) does not clear the registers. 7.3.1 mode 0 (13-bit timer/counter) timer/counter 1 can be configured for three modes by setting m1(tmod.5) and m0(tmod.4) in sepcial function register tmod. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 197 www.stcmcu.com in this mode, the timer register is configured as a 16-bit register. as the count rolls over from all 1s to all 0s, it sets the timer interrupt flag tf1. the counted input is enabled to the timer when tr1 = 1 and either gate=0 or int1 = 1.(setting gate = 1 allows the timer to be controlled by external input int1 , to facilitate pulse width measurements.) trl is a control bit in the special function register tcon. gate is in tmod. the 16-bit register consists of all 8 bits of thl and the lower 8 bits of tl1. setting the run flag (tr1) does not clear the registers. mode 1 is the same as mode 0, except that the timer register is being run with all 16 bits. timer/counter 1 mode 1 : 16-bit timer/counter interrupt sysclk tl1 (8 bits) th1 (8 bits) tf1 control c/t=0 c/t=1 t1 pin tr1 gate int1 auxr.6/t1x12=0 12 1 auxr.6/t1x12=1 7.3.2 mode 1 (16-bit timer/counter) and demo programs (c and asm) 1. c program /*-------------------------------------------------------------------------------*/ /* --- stc mcu international limited ----------------------------------*/ /* --- stc 1t series 16-bit timer demo --------------------------------*/ /* --- mobile: (86)13922809991 ------------------------------------------*/ /* --- fax: 86-755-82905966 ----------------------------------------------*/ /* --- tel: 86-755-82948412 -----------------------------------------------*/ /* --- web: www.stcmcu.com -----------------------------------------*/ /* if you want to use the program or the program referenced in the */ /* article, please specify in which data and procedures from stc */ /*-------------------------------------------------------------------------------*/ #include "reg51.h" typedef unsigned char byte; typedef unsigned int word; there are another two simple programs that demostrates timer 1 as 16-bit timer/counter, one written in c language while other in assembly language. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 198 www.stcmcu.com //----------------------------------------------- /* define constants */ #define fosc 18432000l #define mode1t //timer clock mode, comment this line is 12t mode, uncomment is 1t mode #ifdef mode1t #define t1ms (65536-fosc/1000) //1ms timer calculation method in 1t mode #else #define t1ms (65536-fosc/12/1000) //1ms timer calculation method in 12t mode #endif /* define sfr */ sfr auxr = 0x8e; //auxiliary register sbit test_led = p0^0; //work led, flash once per second /* define variables */ word count; //1000 times counter //----------------------------------------------- /* timer0 interrupt routine */ void tm1_isr() interrupt 3 using 1 { tl1 = t1ms; //reload timer1 low byte th1 = t1ms >> 8; //reload timer1 high byte if (count-- == 0) //1ms * 1000 -> 1s { count = 1000; //reset counter test_led = ! test_led; //work led flash } } //----------------------------------------------- /* main program */ void main() { #ifdef mode1t auxr = 0x40; //timer1 work in 1t mode #endif tmod = 0x10; //set timer1 as mode1 (16-bit) tl1 = t1ms; //initial timer1 low byte th1 = t1ms >> 8; //initial timer1 high byte tr1 = 1; //timer1 start running et1 = 1; //enable timer1 interrupt ea = 1; //open global interrupt switch count = 0; //initial counter while (1); //loop } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 199 www.stcmcu.com 2. assembly program ;/*---------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited ------------------------------------*/ ;/* --- stc 1t series 16-bit timer demo ----------------------------------*/ ;/* --- mobile: (86)13922809991 -------------------------------------------*/ ;/* --- fax: 86-755-82905966 -----------------------------------------------*/ ;/* --- tel: 86-755-82948412 -----------------------------------------------*/ ;/* --- web: www.stcmcu.com ------------------------------------------*/ ;/* if you want to use the program or the program referenced in the */ ;/* article, please specify in which data and procedures from stc */ ;/*------------------------------------------------------------------------------*/ ;/* define constants */ #define mode1t ;timer clock mode, comment this line is 12t mode, uncomment is 1t mode #ifdef mode1t t1ms equ 0b800h ;1ms timer calculation method in 1t mode is (65536-18432000/1000) #else t1ms equ 0fa00h ;1ms timer calculation method in 12t mode is (65536-18432000/12/1000) #endif ;/* define sfr */ auxr data 8eh ;auxiliary register test_led bit p1.0 ;work led, flash once per second ;/* define variables */ count data 20h ;1000 times counter (2 bytes) ;----------------------------------------------- org 0000h ljmp main org 001bh ljmp tm1_isr ;----------------------------------------------- ;/* main program */ main: #ifdef mode1t mov auxr, #40h ;timer1 work in 1t mode #endif mov tmod, #10h ;set timer1 as mode1 (16-bit) mov tl1, #low t1ms ;initial timer1 low byte mov th1, #high t1ms ;initial timer1 high byte setb tr1 ;timer1 start running setb et1 ;enable timer1 interrupt setb ea ;open global interrupt switch clr a mov count, a mov count+1,a ;initial counter sjmp $ stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 200 www.stcmcu.com ;----------------------------------------------- ;/* timer1 interrupt routine */ tm1_isr: push acc push psw mov tl1, #low t1ms ;reload timer1 low byte mov th1, #high t1ms ;reload timer1 high byte mov a, count orl a, count+1 ;check whether count(2byte) is equal to 0 jnz skip mov count, #low 1000 ;1ms * 1000 -> 1s mov count+1,#high 1000 cpl test_led ;work led flash skip: clr c mov a, count ;count-- subb a, #1 mov count, a mov a, count+1 subb a, #0 mov count+1,a pop psw pop acc reti ;----------------------------------------------- end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 201 www.stcmcu.com mode 2 configures the timer register as an 8-bit counter(tl1) with automatic reload. overflow from tl1 not only set tfx, but also reload tl1 with the content of th1, which is preset by software. the reload leaves th1 unchanged. stc12c5a60s2 is able to generate a programmable clock output on p3.5. when t1clko/wake_clko.1 bit in wake_clko sfr is set, t1 timer overflow pulse will toggle p3.5 latch to generate a 50% duty clock. the frequency of clock-out = t1 overflow rate /2. if c/t (tmod.6) = 0, timer/counter 1 is set for timer operation (input from internal system clock), the frequency of clock-out is as following : (sysclk) / (256 C th1) / 2, when auxr.6 / t0x12=1 or (sysclk / 12) / (256 C th1) / 2 , when auxr.6 / t0x12=0 if c/t (tmod.6) = 1, timer/counter 1 is set for conter operation (input from external p3.5/t1 pin), the frequency of clock-out is as following : t1_pin_clk / (256-th1) / 2 timer/counter 1 mode 2: 8-bit auto-reload interrupt sysclk tf1 control c/t=0 c/t=1 t1 pin tr1 gate int1 auxr.6/t1x12=0 auxr.6/t1x12=1 tl1 (8 bits) th1 (8 bits) 12 1 toggle t1clko p3.5 clkout1 7.3.3 mode 2 (8-bit auto-reload mode) and demo programs (c and asm) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 202 www.stcmcu.com ;t1 interrupt (falling edge) demo programs, where t1 operated in mode 2 (8-bit auto-relaod mode) ; the timer interrupt can not wake up mcu from power-down mode in the following programs 1. c program /*-------------------------------------------------------------------------------*/ /* --- stc mcu international limited ----------------------------------*/ /* --- stc 1t series mcu t1(falling edge) demo -------------------*/ /* --- mobile: (86)13922809991 ------------------------------------------*/ /* --- fax: 86-755-82905966 ----------------------------------------------*/ /* --- tel: 86-755-82948412 -----------------------------------------------*/ /* --- web: www.stcmcu.com -----------------------------------------*/ /* if you want to use the program or the program referenced in the */ /* article, please specify in which data and procedures from stc */ /*------------------------------------------------------------------------------*/ #include "reg51.h" sfr auxr = 0x8e; //auxiliary register //t1 interrupt service routine void t1int( ) interrupt 3 //t1 interrupt (location at 001bh) { } void main() { auxr = 0x40; //timer1 work in 1t mode tmod = 0x60; //set timer1 as counter mode2 (8-bit auto-reload) tl1 = th1 = 0xff; //fill with 0xff to count one time tr1 = 1; //timer1 start run et1 = 1; //enable t1 interrupt ea = 1; //open global interrupt switch while (1); } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 203 www.stcmcu.com 2. assembly program /*-------------------------------------------------------------------------------*/ /* --- stc mcu international limited ----------------------------------*/ /* --- stc 1t series mcu t1(falling edge) demo -------------------*/ /* --- mobile: (86)13922809991 ------------------------------------------*/ /* --- fax: 86-755-82905966 ----------------------------------------------*/ /* --- tel: 86-755-82948412 -----------------------------------------------*/ /* --- web: www.stcmcu.com -----------------------------------------*/ /* if you want to use the program or the program referenced in the */ /* article, please specify in which data and procedures from stc */ /*------------------------------------------------------------------------------*/ auxr data 08eh ;auxiliary register ;----------------------------------------- ;interrupt vector table org 0000h ljmp main org 001bh ;t1 interrupt (location at 001bh) ljmp t1int ;----------------------------------------- org 0100h main: mov sp, #7fh ;initial sp mov auxr, #40h ;timer1 work in 1t mode mov tmod, #60h ;set timer1 as counter mode2 (8-bit auto-reload) mov a, #0ffh mov tl1, a ;fill with 0xff to count one time mov th1, a setb tr1 ;timer1 start run setb et1 ;enable t1 interrupt setb ea ;open global interrupt switch sjmp $ ;----------------------------------------- ;t1 interrupt service routine t1int: reti ;----------------------------------------- end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 204 www.stcmcu.com 7.4 programmable clock output and demo programs (c and asm) stc12c5a60s2 series mcu have three channel programmable clock outputs, they are timer 0 programmable clock output clkout0(p3.4/t0), timer 1 programmable clock output clkout1(p3.5/t1) and dedicated baud-rate timer programmable clock output (clkout2/p1.0). there are some sfrs about programmable clock output as shown below. the satement (used in c language) of special function registers auxr/wake_clko/brt: sfr auxr = 0x8e; //the address statement of special function register auxr sfr wake_clko = 0x8f; //the address statement of sfr wake_clko sfr brt = 0x9c; //the address statement of special function register brt the satement (used in assembly language) of special function registers auxr/wake_clko/brt: auxr equ 0x8e ;the address statement of special function register auxr wake_clko equ 0x8f ;the address statement of sfr wake_clko brt equ 0x9c ;the address statement of special function register brt symbol description address bit address and symbol msb lsb value after power-on or reset auxr auxiliary register 8eh t0x12 t1x12 uart_m0x6 brtr s2smod brtx12 extram s1brs 0000 0000b wake_clko clk_output power down wake-up control register 8fh pcawakeup rxd_pin_ie t1_pin_ie t0_pin_ie lvd_wake brtclko t1clko t0clko 0000 0000b brt dedicated baud- rate timer register 9ch 0000 0000b 1. auxr: auxiliary register (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 auxr 8eh name t0x12 t1x12 uart_m0x6 brtr s2smod brtx12 extram s1brs t0x12 : timer 0 clock source bit. 0 : the clock source of timer 0 is sysclk/12. it will compatible to the traditional 80c51 mcu 1 : the clock source of timer 0 is sysclk/1. it will drive the t0 faster than a traditional 80c51 mcu t1x12 : timer 1 clock source bit. 0 : the clock source of timer 1 is sysclk/12. it will compatible to the traditional 80c51 mcu 1 : the clock source of timer 1 is sysclk/1. it will drive the t0 faster than a traditional 80c51 mcu uart_m0x6 : baud rate select bit of uart1 while it is working under mode-0 0 : the baud-rate of uart in mode 0 is sysclk/12. 1 : the baud-rate of uart in mode 0 is sysclk/2. brtr : dedicated baud-rate timer run control bit. 0 : the baud-rate generator is stopped. 1 : the baud-rate generator is enabled. s2smod : the baud-rate of uart2 double contol bit. 0 : default. the baud-rate of uart2 (s2) is not doubled. 1 : the baud-rate uart2 (s2) is doubled. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 205 www.stcmcu.com brtx12 : dedicated baud-rate timer counter control bit. 0 : the baud-rate generator is incremented every 12 system clocks. 1 : the baud-rate generator is incremented every system clock. extram : internal / external ram access control bit. 0 : on-chip auxiliary ram is enabled and located at the address 0x0000 to 0x03ff. for address over 0x03ff, off-chip expanded ram becomes the target automatically. 1 : on-chip auxiliary ram is always disabled. s1brs : the baud-rate generator of uart1 select bit. 0 : default. select timer 1 as the baud-rate generator of uart1 1 : timer 1 is replaced by the independent baud-rate generator which is selected as the baud-rate of uart. in other words, timer 1 is released to use in other functions. 2. wake_clko: clk_output power down wake-up control register (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 wake_clko 8fh name pcawakeup rxd_pin_ie t1_pin_ie t0_pin_ie lvd_wake brtclko t1clko t0clko pcawakeup: when set and the associated-pca interrupt control registers is configured correctly, the cexn pin of pca function is enabled to wake up mcu from power-down state. rxd_pin_ie: when set and the associated-uart interrupt control registers is configured correctly, the rxd pin (p3.0) is enabled to wake up mcu from power-down state. t1_pin_ie : when set and the associated-timer1 interrupt control registers is configured correctly, the t1 pin (p3.5) is enabled to wake up mcu from power-down state. t0_pin_ie : when set and the associated-timer0 interrupt control registers is configured correctly, the t1 pin (p3.4) is enabled to wake up mcu from power-down state. lvd_wake: when set and the associated-lvd interrupt control registers is configured correctly, the cmpin pin is enabled to wake up mcu from power-down state. brtcklo : when set, p1.0 is enabled to be the clock output of baud-rate timer (brt). the clock rate is brg overflow rate divided by 2. t1cklo : when set, p3.5 is enabled to be the clock output of timer 1. the clock rate is timer 1overflow rate divided by 2. t0cklo : when set, p3.4 is enabled to be the clock output of timer 0. the clock rate is timer 0overflow rate divided by 2. 3. brt: dedicated baud-rate timer register (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 brt 9ch name it is used as the reload register for generating the baud-rate of the uart. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 206 www.stcmcu.com 7.4.1 timer 0 programmable clock-out on p3.4 and demo program(c and asm) timer/counter 0 mode 2: 8-bit auto-reload sysclk control c/t=0 c/t=1 t0 pin tr0 gate int0 auxr.7/t0x12=0 auxr.7/t0x12=1 tl0 (8 bits) th0 (8 bits) 12 1 interrupt tf0 toggle t0clko p3.4 clkout0 stc12c5a60s2 is able to generate a programmable clock output on p3.4. when t0clko/ wake_clko.0 bit in wake_clko sfr is set, t0 timer overflow pulse will toggle p3.4 latch to generate a 50% duty clock. the frequency of clock-out = t0 overflow rate /2. if c/t (tmod.2) = 0, timer/counter 0 is set for timer operation (input from internal system clock), the frequency of clock-out is as following : (sysclk) / (256 C th0) / 2, when auxr.7 / t0x12=1 or (sysclk / 12) / (256 C th0) / 2 , when auxr.7 / t0x12=0 if c/t (tmod.2) = 1, timer/counter 0 is set for conter operation (input from external p3.4/t0 pin), the frequency of clock-out is as following : t0_pin_clk / (256-th0) / 2 the following programs demostrate program clock output on timer 0 pin when timer 0 operates as 8-bit auto- reload timer/counter. 1. c program: /*--------------------------------------------------------------------------------*/ /* --- stc mcu international limited -----------------------------------*/ /* --- stc 1t series programmable clock output demo -------------*/ /* --- mobile: (86)13922809991 -------------- ----------------------------*/ /* --- fax: 86-755-82905966 ----------------------------------------------*/ /* --- tel: 86-755-82948412 -----------------------------------------------*/ /* --- web: www.stcmcu.com -----------------------------------------*/ /* if you want to use the program or the program referenced in the */ /* article, please specify in which data and procedures from stc */ /*-------------------------------------------------------------------------------*/ #include "reg51.h" //----------------------------------------------- /* define constants */ #define fosc 18432000l //#define mode 1t //timer clock mode, comment this line is 12t mode, uncomment is 1t mode stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 207 www.stcmcu.com #ifdef mode 1t #define f38_4khz (256-fosc/2/38400) //38.4khz frequency calculation method of 1t mode #else #define f38_4khz (256-fosc/2/12/38400) //38.4khz frequency calculation method of 12t mode #endif /* define sfr */ sfr auxr = 0x8e; //auxiliary register sfr wake_clko = 0x8f; //wakeup and clock output control register sbit t0clko = p3^4; //timer0 clock output pin //----------------------------------------------- /* main program */ void main() { #ifdef mode1t auxr = 0x80; //timer0 work in 1t mode #endif tmod = 0x02; //set timer0 as mode2 (8-bit auto-reload) tl0 = f38_4khz; //initial timer0 th0 = f38_4khz; //initial timer0 tr0 = 1; //timer0 start running wake_clko = 0x01; //enable timer0 clock output while (1); //loop } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 208 www.stcmcu.com 2. assembly program: ;/*--------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited -----------------------------------*/ ;/* --- stc 1t series programmable clock output demo -------------*/ ;/* --- mobile: (86)13922809991 -------------- ----------------------------*/ ;/* --- fax: 86-755-82905966 -----------------------------------------------*/ ;/* --- tel: 86-755-82948412 -----------------------------------------------*/ ;/* --- web: www.stcmcu.com ------------------------------------------*/ ;/* if you want to use the program or the program referenced in the */ ;/* article, please specify in which data and procedures from stc */ ;/*-------------------------------------------------------------------------------*/ ;/* define constants */ #define mode 1t ;timer clock mode, comment this line is 12t mode, uncomment is 1t mode #ifdef mode 1t f38_4khz equ 010h ;38.4khz frequency calculation method of 1t mode is (256-18432000/2/38400) #else f38_4khz equ 0ech ;38.4khz frequency calculation method of 12t mode (256-18432000/2/12/38400) #endif ;/* define sfr */ auxr data 08eh ;auxiliary register wake_clko data 08fh ;wakeup and clock output control register t0clko bit p3.4 ;timer0 clock output pin ;----------------------------------------------- org 0000h ljmp main ;----------------------------------------------- ;/* main program */ main: #ifdef mode1t mov auxr, #80h ;timer0 work in 1t mode #endif mov tmod, #02h ;set timer0 as mode2 (8-bit auto-reload) mov tl0, #f38_4khz ;initial timer0 mov th0, #f38_4khz ;initial timer0 setb tr0 mov wake_clko, #01h ;enable timer0 clock output sjmp $ ;----------------------------------------------- end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 209 www.stcmcu.com 7.4.2 timer 1 programmable clock-out on p3.5 and demo program(c and asm) stc12c5a60s2 is able to generate a programmable clock output on p3.5. when t1clko/wake_clko.1 bit in wake_clko sfr is set, t1 timer overflow pulse will toggle p3.5 latch to generate a 50% duty clock. the frequency of clock-out = t1 overflow rate /2. if c/t (tmod.6) = 0, timer/counter 1 is set for timer operation (input from internal system clock), the frequency of clock-out is as following : (sysclk) / (256 C th1) / 2, when auxr.6 / t0x12=1 or (sysclk / 12) / (256 C th1) / 2 , when auxr.6 / t0x12=0 if c/t (tmod.6) = 1, timer/counter 1 is set for conter operation (input from external p3.5/t1 pin), the frequency of clock-out is as following : t1_pin_clk / (256-th1) / 2 timer/counter 1 mode 2: 8-bit auto-reload interrupt sysclk tf1 control c/t=0 c/t=1 t1 pin tr1 gate int1 auxr.6/t1x12=0 auxr.6/t1x12=1 tl1 (8 bits) th1 (8 bits) 12 1 toggle t1clko p3.5 clkout1 the following programs demostrate program clock output on timer 1 pin when timer 1 operates as 8-bit auto- reload timer/counter. 1. c program: /*-------------------------------------------------------------------------------*/ /* --- stc mcu international limited ----------------------------------*/ /* --- stc 1t series programmable clock output demo ------------*/ /* --- mobile: (86)13922809991 -------------- ---------------------------*/ /* --- fax: 86-755-82905966 ----------------------------------------------*/ /* --- tel: 86-755-82948412 -----------------------------------------------*/ /* --- web: www.stcmcu.com -----------------------------------------*/ /* if you want to use the program or the program referenced in the */ /* article, please specify in which data and procedures from stc */ /*-------------------------------------------------------------------------------*/ #include "reg51.h" //----------------------------------------------- stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 210 www.stcmcu.com /* define constants */ #define fosc 18432000l //#define mode 1t //timer clock mode, comment this line is 12t mode, uncomment is 1t mode #ifdef mode 1t #define f38_4khz (256-fosc/2/38400) //38.4khz frequency calculation method of 1t mode #else #define f38_4khz (256-fosc/2/12/38400) //38.4khz frequency calculation method of 12t mode #endif /* define sfr */ sfr auxr = 0x8e; //auxiliary register sfr wake_clko = 0x8f; //wakeup and clock output control register sbit t1clko = p3^5; //timer1 clock output pin //----------------------------------------------- /* main program */ void main() { #ifdef mode1t auxr = 0x40; //timer1 work in 1t mode #endif tmod = 0x20; //set timer1 as mode2 (8-bit auto-reload) tl1 = f38_4khz; //initial timer1 th1 = f38_4khz; //initial timer1 tr1 = 1; //timer1 start running wake_clko = 0x02; //enable timer1 clock output while (1); //loop } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 211 www.stcmcu.com 2. assembly program: ;/*---------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited ------------------------------------*/ ;/* --- stc 1t series programmable clock output demo --------------*/ ;/* --- mobile: (86)13922809991 -------------------------------------------*/ ;/* --- fax: 86-755-82905966 -----------------------------------------------*/ ;/* --- tel: 86-755-82948412 ------------------------------------------------*/ ;/* --- web: www.stcmcu.com ------------------------------------------*/ ;/* if you want to use the program or the program referenced in the */ ;/* article, please specify in which data and procedures from stc */ ;/*-------------------------------------------------------------------------------*/ ;/* define constants */ #define mode 1t ;timer clock mode, comment this line is 12t mode, uncomment is 1t mode #ifdef mode 1t f38_4khz equ 010h ;38.4khz frequency calculation method of 1t mode is (256-18432000/2/38400) #else f38_4khz equ 0ech ;38.4khz frequency calculation method of 12t mode (256-18432000/2/12/38400) #endif ;/* define sfr */ auxr data 08eh ;auxiliary register wake_clko data 08fh ;wakeup and clock output control register t1clko bit p3.5 ;timer1 clock output pin ;----------------------------------------------- org 0000h ljmp main ;----------------------------------------------- ;/* main program */ main: #ifdef mode1t mov auxr, #40h ;timer1 work in 1t mode #endif mov tmod, #20h ;set timer1 as mode2 (8-bit auto-reload) mov tl1, #f38_4khz ;initial timer1 mov th1, #f38_4khz ;initial timer1 setb tr1 mov wake_clko, #02h ;enable timer1 clock output sjmp $ ;----------------------------------------------- end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 212 www.stcmcu.com 7.4.3 baud rate generator programmable clock output on p1.0 and demo program 8 bits timer brt toggle brtclko p1.0 stc12c5a60s2 is able to generate a programmable clock output on p1.0. when brtclko bit in wake_clko is set, brt timer overflow pulse will toggle p1.0 latch to generate a 50% duty clock. the frequency of clock- out = baud-rate timer overflow rate /2. namely the frequency of clock-out is shown as below : (sysclk) / (256 Cbrt) /2, when brtx12=1 or (sysclk/12) / (256 C brt) /2 , when brtx12=0 sysclk auxr.2/brtx12=0 auxr.2/brtx12=1 12 1 auxr.4 / brtr clkout2 the following program is a assembly language code that domestrates timer 1 of stc12c5a60s2 series mcu acted as baud rate generator. ;/*------------------------------------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited ---------------------------------------------------------------*/ ;/* --- stc 1t series mcu timer 1 acted as baud rate generator demo -----------------------*/ ;/* --- mobile: (86)13922809991 -----------------------------------------------------------------------*/ ;/* --- fax: 86-755-82905966 ---------------------------------------------------------------------------*/ ;/* --- tel: 86-755-82948412 ----------------------------------------------------------------------------*/ ;/* --- web: www.stcmcu.com -----------------------------------------------------------------------*/ ;/* if you want to use the program or the program referenced in the */ ;/* article, please specify in which data and procedures from stc */ ;/*--------------------------------------------------------------------------------------------------------------*/ ;declare stc11/10xx series mcu sfr auxr equ 8eh ;*/--------------------------------------------------------------------------------------------------------------*/ ;define baud rate auto-reload counter ;**************************************************************************** ;the following reload-count and baud is based on sysclk =22.1184mhz, 1t mode, smod=1 ;reload_count equ 0ffh ;baud=1,382,400 bps ;reload_count equ 0feh ;baud=691,200 bps ;reload_count equ 0fdh ;baud=460,800 bps ;reload_count equ 0fch ;baud=345,600 bps ;reload_count equ 0fbh ;baud=276,480 bps ;reload_count equ 0fah ;baud=230,400 bps ;reload_count equ 0f4h ;baud=115,200 bps ;reload_count equ 0e8h ;baud=57,600 bps ;reload_count equ 0dch ;baud=38,400 bps ;reload_count equ 0b8h ;baud=19,200 bps ;reload_count equ 70h ;baud=9,600 bps to uart overflow stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 213 www.stcmcu.com ;******************************************************************************* ;the following reload-count and baud is based on sysclk =1.8432mhz, 1t mode, smod=1 ;reload_count equ 0ffh ;baud=115,200 bps ;reload_count equ 0feh ;baud=57,600 bps ;reload_count equ 0fdh ;baud=38,400 bps ;reload_count equ 0fch ;baud=28,800 bps ;reload_count equ 0fah ;baud=19,200 bps ;reload_count equ 0f4h ;baud=9,600 bps ;reload_count equ 0e8h ;baud=4,800 bps ;reload_count equ 0d0h ;baud=2,400 bps ;reload_count equ 0a0h ;baud=1,200 bps ;******************************************************************************** ;the following reload-count and baud is based on sysclk =18.432mhz, 1t mode, smod=1 ;reload_count equ 0ffh ;baud=1,152,000 bps ;reload_count equ 0feh ;baud=576,000 bps ;reload_count equ 0fdh ;baud=288,000 bps ;reload_count equ 0fch ;baud=144,000 bps ;reload_count equ 0f6h ;baud=115,200 bps ;reload_count equ 0ech ;baud=57,600 bps ;reload_count equ 0e2h ;baud=38,400 bps ;reload_count equ 0d8h ;baud=28,800 bps ;reload_count equ 0c4h ;baud=19,200 bps ;reload_count equ 088h ;baud=9,600 bps ;******************************************************************************** ;the following reload-count and baud is based on sysclk =18.432mhz, 1t mode, smod=0 ;reload_count equ 0ffh ;baud=576,000 bps ;reload_count equ 0feh ;baud=288,000 bps ;reload_count equ 0fdh ;baud=144,000 bps ;reload_count equ 0fch ;baud=115,200 bps ;reload_count equ 0f6h ;baud=57,600 bps ;reload_count equ 0ech ;baud=38,400 bps ;reload_count equ 0e2h ;baud=28,800 bps ;reload_count equ 0d8h ;baud=19,200 bps ;reload_count equ 0c4h ;baud=96,000 bps ;reload_count equ 088h ;baud=4,800 bps ;********************************************************************************* ;the following reload-count and baud is based on sysclk =18.432mhz, 12t mode, smod=0 reload_count equ 0fbh ;baud=9,600 bps ;reload_count equ 0f6h ;baud=4,800 bps ;reload_count equ 0ech ;baud=2,400 bps ;reload_count equ 0d8h ;baud=1,200 bps ;********************************************************************************* ;the following reload-count and baud is based on sysclk =18.432mhz, 12t mode, smod=1 reload_count equ 0fbh ;baud=19,200 bps ;reload_count equ 0f6h ;baud=9,600 bps ;reload_count equ 0ech ;baud=4,800 bps ;reload_count equ 0d8h ;baud=2,400 bps ;reload_count equ 0b0h ;baud=1,200 bps stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 214 www.stcmcu.com ;******************************************************************************** ;the following reload-count and baud is based on sysclk =11.0592mhz, 12t mode, smod=0 ;reload_count equ 0ffh ;baud=28,800 bps ;reload_count equ 0feh ;baud=14,400 bps ;reload_count equ 0fdh ;baud=9,600 bps ;reload_count equ 0fah ;baud=4,800 bps ;reload_count equ 0f4h ;baud=2,400 bpss ;reload_count equ 0e8h ;baud=1,200 bps ;******************************************************************************** ;******************************************************************************** ;the following reload-count and baud is based on sysclk =11.0592mhz, 12t mode, smod=1 ;reload_count equ 0ffh ;baud=57,600 bps ;reload_count equ 0feh ;baud=28,800 bps ;reload_count equ 0fdh ;baud=14,400 bps ;reload_count equ 0fah ;baud=9,600 bps ;reload_count equ 0f4h ;baud=4,800 bps ;reload_count equ 0e8h ;baud=2,400 bps ;reload_count equ 0d0h ;baud=1,200 bps ;******************************************************************************** ;define led indicator led_mcu_start equ p1.7 ;mcu operating led indicator ;------------------------------------------------------------------------------------------------------------------------- org 0000h ajmp main ;------------------------------------------------------------------------------------------------------------------------- org 0023h ajmp uart_interrupt ;jump into rs232 uart-interrupt service subroutine nop nop ;-------------------------------------------------------------------------------------------------------------------------- main: mov sp, #7fh ;set stack pointer clr led_mcu_start ;open mcu operating led indicator acall initial_uart ;initialize uart mov r0, #30h ;30h = the ascii code of printable character '0' mov r2, #10 ;send ten characters '0123456789' loop: mov a, r0 acall send_one_byte ;send one byte ; if character-display, display '0123456789' ;if hexadecimal-display, display '30 31 32 33 34 35 36 37 38 39' inc r0 djnz r2, loop main_wait: sjmp main_wait ;infinite circle ;---------------------------------------------------------------------------------------------------------------------------- stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 215 www.stcmcu.com uart_interrupt: ;uart-interrupt service subroutine jb ri, is_uart_receive clr ti ;clear serial port transmit interrupt flag reti is_uart_receive: clr ri push acc mov a, sbuf ;acquire the received byte acall send_one_byte ;re-send the received byte pop acc reti ;----------------------------------------------------------------------------------------------------------------------- initial_uart: ;initialize uart ;scon bit: 7 6 5 4 3 2 1 0 ; sm0/fe sm1 sm2 ren tb8 rb8 ti ri mov scon, #50h ;0101,0000 8-bit variable baud rate,no odd parity bit mov tmod, #21h ;use timer 1 as 8 bit auto-reload counter mov th1, #reload_count ;set auto-reload count of timer 1 mov tl1, #reload_count ;---------------------------------------------------------------------------------------------------------------------- ; orl pcon, #80h ;baud rate double ;---------------------------------------------------------------------------------------------------------------------- ; orl auxr, #01000000b ;use timer 1 in 1t mode anl auxr, #10111111b ;use timer 1 in 12t mode ;---------------------------------------------------------------------------------------------------------------------- setb r1 ; start up timer 1 setb es setb ea ret ;----------------------------------------------------------------------------------------------------------------------- ;portal parameter: a= the byte to send send_one_byte: ;send one byte clr es clr ti ;clear serial port transmit interrupt flag mov sbuf, a wait_send_finish: jnb ti, wait_send_finish ;wait to finish send clr ti setb es ret ;-------------------------------------------------------------------------------------------------------------------------\ end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 216 www.stcmcu.com the example program that demostrates programmable clock out as follows: /* sysclk = 18.432mhz; t0,t1 and independent baud rate generator all in 1t mode. */ #include "reg51.h" sfr wake_clko = 0x8f; sfr auxr = 0x8e; sfr brt = 0x9c; main ( ) { tmod = 0x22; // t0 and t1 all in mode 2, 8-bit auto-reload counter auxr = (auxr | 0x80); // t0 in 1t mode auxr = (auxr | 0x40); // t1 in 1t mode auxr = (auxr | 0x04); // dedicated baud-rate timer in 1t mode brt = (256-74); //8-bit reload value in brt, sysclko is 124.540khz th0 = (256-74); //8-bit reload value in th0,sysclko=18432000/2/74=124540.54 th1 = (256-240); //8-bit reload value in th1, sysclko=18432000/2/240=38400 wake_clko = ( wake_clko | 0x07); //allow t0, t1 and independent baud rate generator output clock tr0 = 1; //start timer 0 as couter, system clock is divided and output tr1 = 1; //start timer 1 as counter, system clock is divided and output auxr = (auxr | 0x10); //start independent baud rate generator as counter //system clock has been output and could be watched through oscilloscope while(1); } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 217 www.stcmcu.com 1. c program: /*--------------------------------------------------------------------------------*/ /* --- stc mcu international limited -----------------------------------*/ /* --- stc 1t series programmable clock output demo -------------*/ /* --- mobile: (86)13922809991 -------------- ---------------------------*/ /* --- fax: 86-755-82905966 ----------------------------------------------*/ /* --- tel: 86-755-82948412 -----------------------------------------------*/ /* --- web: www.stcmcu.com -----------------------------------------*/ /* if you want to use the program or the program referenced in the */ /* article, please specify in which data and procedures from stc */ /*------------------------------------------------------------------------------*/ #include "reg51.h" //----------------------------------------------- /* define constants */ #define fosc 18432000l //#define mode1t //timer clock mode, comment this line is 12t mode, uncomment is 1t mode #ifdef mode1t #define f38_4khz (256-fosc/2/38400) //38.4khz frequency calculation method of 1t mode #else #define f38_4khz (256-fosc/2/12/38400) //38.4khz frequency calculation method of 12t mode #endif /* define sfr */ sfr auxr = 0x8e; //auxiliary register sfr wake_clko = 0x8f; //wakeup and clock output control register sfr brt = 0x9c; sbit brtclko = p1^0; //brt clock output pin //----------------------------------------------- /* main program */ void main() { #ifdef mode1t auxr = 0x04; //brt work in 1t mode #endif brt = f38_4khz; //initial brt auxr |= 0x10; //brt start running wake_clko = 0x04; //enable brt clock output while (1); //loop } the following programs demostrate program clock output of independent baud raud timer on p1.0 pin stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 218 www.stcmcu.com ;/* define sfr */ auxr data 08eh ;auxiliary register wake_clko data 08fh ;wakeup and clock output control register brt data 09ch brtclko bit p1.0 ;brt clock output pin ;----------------------------------------------- org 0000h ljmp main ;----------------------------------------------- ;/* main program */ main: #ifdef mode1t mov auxr, #04h ;brt work in 1t mode #endif mov brt, #f38_4khz ;initial brt reload value orl auxr, #10h ;brt start run mov wake_clko, #04h ;enable brt clock output sjmp $ ;----------------------------------------------- end 2. assembly program: ;/*-------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited ----------------------------------*/ ;/* --- stc 1t series programmable clock output demo ------------*/ ;/* --- mobile: (86)13922809991 ------------------------------------------*/ ;/* --- fax: 86-755-82905966 ----------------------------------------------*/ ;/* --- tel: 86-755-82948412 -----------------------------------------------*/ ;/* --- web: www.stcmcu.com -----------------------------------------*/ ;/* if you want to use the program or the program referenced in the */ ;/* article, please specify in which data and procedures from stc */ ;/*-------------------------------------------------------------------------------*/ ;/* define constants */ #define mode1t ;timer clock mode, comment this line is 12t mode, uncomment is 1t mode #ifdef mode1t f38_4khz equ 010h ;38.4khz frequency calculation method of 1t mode is (256-18432000/2/38400) #else f38_4khz equ 0ech ;38.4khz frequency calculation method of 12t mode (256-18432000/2/12/38400) #endif stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 219 www.stcmcu.com clr ea ;disable interrupt mov a, tlx ;read tlx add a, #low ;low is low byte of compensation value mov tlx, a ;update tlx mov a, thx ;read thx addc a, #high ;high is high byte of compensation value mov thx, a ;update thx setb ea ;enable interrupt (2) dynamic read counts when dynamic read running timer count value, if you do not pay attention to could be wrong, this is because it is not possible at the same time read the value of the tlx and thx. for example the first reading tlx then thx, because the timer is running, after reading tlx, tlx carry on the thx produced, resulting in error; similarly, after the first reading of thx then tlx, also have the same problems. a kind of way avoid reading wrong is first reading thx then tlx and read thx once more, if the thx twice to read the same value, then the read value is correct, otherwise repeat the above process. realization method reference to the following example code. rdtm: mov a, thx ;save thx to acc mov r0, tlx ;save tlx to r0 cjne a, thx, rdtm ;read thx again and compare with the previous value mov r1, a ;save thx to r1 7.5 application notes for timer in practice (1) real-time timer timer/counter start running, when the timer/counter is overflow, the interrupt request generated, this action handle by the hardware automatically, however, the process which from propose interrupt request to respond interrupt request requires a certain amount of time, and that the delay interrupt request on-site with the environment varies, it normally takes three machine cycles of delay, which will bring real-time processing bias. in most occasions, this error can be ignored, but for some real-time processing applications, which require compensation. such as the interrupt response delay, for timer mode 0 and mode 1, there are two meanings: the first, because of the interrupt response time delay of real-time processing error; the second, if you require multiple consecutive timing, due to interruption response delay, resulting in the interrupt service program once again sets the count value is delayed by several count cycle. if you choose to use timer/counter mode 1 to set the system clock, these reasons will produce real-time error for this situation, you should use dynamic compensation approach to reducing error in the system clock, compensation method can refer to the following example program. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 220 www.stcmcu.com chapter 8. serial interface (uart) 8.1 uart with enhanced function stc12c5a60s2 series mcu have two universal asychronous receivers/transmitters serial port 1 and serial port 2. the serial ports are both full duplex, meaning they can transmit and receive simultane - ously. they are also receive-buffered, meaning they can commence reception of a second byte before a previ - ously received byte has been read from the reeeive register. (however,if the first byte still hasnt been read by the time reception of the second byte is complete, one of the bytes will be lost). the serial ports 1 receive and transmit share the same sfr C sbuf, but actually there is two sbuf in the chip, one is for transmit and the other is for receive. similarly, the serial ports 2 receive and transmit share the same sfr C s2buf which also have two s2buf in the chip, one for transmit and the other for receive. the serial ports(uart1 and uart2) can be both operated in 4 different modes: mode 0 provides synchronous communication while modes 1, 2, and 3 provide asynchronous communication. the asynchronous communication operates as a full-duplex universal asynchronous receiver and transmitter (uart), which can transmit and receive simultaneously and at different baud rates. serial communiction involves the transimission of bits of data through only one communication line. the data are transimitted bit by bit in either synchronous or asynchronous format. synchronous serial communication transmits ont whole block of characters in syschronization with a reference clock while asynchronous serial communication randomly transmits one character at any time, independent of any clock. symbol description address bit address and symbol msb lsb value after power-on or reset brt baud-rate timer 9ch 0000 0000b auxr auxiliary register 8eh t0x12 t1x12 uart_m0x6 brtr s2smod brtx12 extram s1brs 0000 0000b scon serial port control 98h sm0/fe sm1 sm2 ren tb8 rb8 ti ri 0000 0000b sbuf serial port buffer 99h xxxx xxxxb pcon power control 87h smod smod0 lvdf pof gf1 gf0 pd idl 0001 0000b ie interrupt enable a8h ea elvd eadc es et1 ex1 et0 ex0 0x00 0000b ip interrupt priority low b8h ppca plvd padc ps pt1 px1 pt0 px0 0000 0000b iph interrupt priority high b7h ppcah plvdh padch psh pt1h px1h pt0h px0h 0000 0000b saden slave address mask b9h 0000 0000b saddr slave address a9h 0000 0000b wake_clko clk_output power down wake-up control register 8fh pcawakeup rxd_pin_ie t1_pin_ie t0_pin_ie lvd_wake brtclko t1clko t0clko 0000 0000b 8.1.1 special function registers about uart1 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 221 www.stcmcu.com 1. serial port 1 (uart1) control register: scon and pcon serial port 1 of stc12c5a60s2 series has two control registers: serial port control register (scon) and pcon which used to select baud-rate scon: serial port control register (bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 scon 98h name sm0/fe sm1 sm2 ren tb8 rb8 ti ri fe: framing error bit. the smod0 bit must be set to enable access to the fe bit 0: the fe bit is not cleared by valid frames but should be cleared by software. 1: this bit set by the receiver when an invalid stop bit id detected. sm0,sm1 : serial port mode bit 0/1. sm0 sm1 description baud rate 0 0 8-bit shift register sysclk/12 0 1 8-bit uart variable 1 0 9-bit uart sysclk/64 or sysclk/32(smod=1) 1 1 9-bit uart variable sm2 : enable the automatic address recognition feature in mode 2 and 3. if sm2=1, ri will not be set unless the received 9th data bit is 1, indicating an address, and the received byte is a given or broadcast address. in mode1, if sm2=1 then ri will not be set unless a valid stop bit was received, and the received byte is a given or broadcast address. in mode 0, sm2 should be 0. ren : when set enables serial reception. tb8 : the 9th data bit which will be transmitted in mode 2 and 3. rb8 : in mode 2 and 3, the received 9th data bit will go into this bit. ti : transmit interrupt flag. set by hardware when a byte of data has been transmitted by uart0 (after the 8th bit in 8-bit uart mode, or at the beginning of the stop bit in 9-bit uart mode). when the uart0 in - terrupt is enabled, setting this bit causes the cpu to vector to the uart0 interrupt service routine. this bit must be cleared manually by software. ri : receive interrupt flag. set to 1 by hardware when a byte of data has been received by uart0 (set at the stop bit sam-pling time). when the uart0 interrupt is enabled, setting this bit to 1 causes the cpu to vector to the uart0 interrupt service routine. this bit must be cleared manually by software. smod/pcon.7 in pcon register can be used to set whether the baud rates of mode 1, mode2 and mode 3 are doubled or not. pcon: power control register (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 pcon 87h name smod smod0 lvdf pof gf1 gf0 pd idl smod: double baud rate control bit. 0 : disable double baud rate of the uart. 1 : enable double baud rate of the uart in mode 1,2,or 3. smod0: frame error select. 0 : scon.7 is sm0 function. 1 : scon.7 is fe function. note that fe will be set after a frame error regardless of the state of smod0. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 222 www.stcmcu.com 2. sbuf: serial port 1 data buffer register (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 sbuf 99h name it is used as the buffer register in transmission and reception.the serial port buffer register (sbuf) is really two buffers. writing to sbuf loads data to be transmitted, and reading sbuf accesses received data. these are two separate and distinct registers, the transimit write-only register, and the receive read-only register. 3. auxr: auxiliary register (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 auxr 8eh name t0x12 t1x12 uart_m0x6 brtr s2smod brtx12 extram s1brs t0x12 : timer 0 clock source bit. 0 : the clock source of timer 0 is sysclk/12. it will compatible to the traditional 80c51 mcu 1 : the clock source of timer 0 is sysclk/1. it will drive the t0 faster than a traditional 80c51 mcu t1x12 : timer 1 clock source bit. 0 : the clock source of timer 1 is sysclk/12. it will compatible to the traditional 80c51 mcu 1 : the clock source of timer 1 is sysclk/1. it will drive the t0 faster than a traditional 80c51 mcu uart_m0x6 : baud rate select bit of uart1 while it is working under mode-0 0 : the baud-rate of uart in mode 0 is sysclk/12. 1 : the baud-rate of uart in mode 0 is sysclk/2. brtr : dedicated baud-rate timer run control bit. 0 : the baud-rate generator is stopped. 1 : the baud-rate generator is enabled. s2smod : the baud-rate of uart2 double contol bit. 0 : default. the baud-rate of uart2 (s2) is not doubled. 1 : the baud-rate uart2 (s2) is doubled. brtx12 : dedicated baud-rate timer counter control bit. 0 : the baud-rate generator is incremented every 12 system clocks. 1 : the baud-rate generator is incremented every system clock. extram : internal / external ram access control bit. 0 : on-chip auxiliary ram is enabled and located at the address 0x0000 to 0x03ff. for address over 0x03ff, off-chip expanded ram becomes the target automatically. 1 : on-chip auxiliary ram is always disabled. s1brs : the baud-rate generator of uart1 select bit. 0 : default. select timer 1 as the baud-rate generator of uart1 1 : timer 1 is replaced by the independent baud-rate generator which is selected as the baud-rate of uart. in other words, timer 1 is released to use in other functions. seial port 1(uart1) can select timer 1, also can select independent baud-rate generator as its baud-rate generator. when s1brs/auxr.0 (the baud-rate generator of uart1) is set, seial port 1(uart1) will select independent baud-rate generator as its baud-rate generator, and timer 1 can be released for other functions such as timer, counter and programmable clock output. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 223 www.stcmcu.com 4. brt: dedicated baud-rate timer register (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 brt 9ch name it is used as the reload register for generating the baud-rate of the uart. 5. slave address control registers saden and saddr saden: slave address mask register saddr: slave address register saddr register is combined with saden register to form given/broadcast address for automatic address recognition. in fact, saden function as the "mask" register for saddr register. the following is the example for it. saddr = 1100 0000 saden = 1111 1101 given = 1100 00x0 the given slave address will be checked except bit 1 is treated as "don't care". the broadcast address for each slave is created by taking the logical or of saddr and saden. zero in this result is considered as "don't care" and a broad cast address of all " don't care". this disables the automatic address detection feature. 6. power down wake-up register : wake_clko (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 wake_clko 8fh name pcawakeup rxd_pin_ie t1_pin_ie t0_pin_ie lvd_wake brtcklo t1cklo t0cklo pcawakeup: when set and the associated-pca interrupt control registers is configured correctly, the cexn pin of pca function is enabled to wake up mcu from power-down state. rxd_pin_ie: when set and the associated-uart interrupt control registers is configured correctly, the rxd pin (p3.0) is enabled to wake up mcu from power-down state. t1_pin_ie : when set and the associated-timer1 interrupt control registers is configured correctly, the t1 pin (p3.5) is enabled to wake up mcu from power-down state. t0_pin_ie : when set and the associated-timer0 interrupt control registers is configured correctly, the t1 pin (p3.4) is enabled to wake up mcu from power-down state. lvd_wake: when set and the associated-lvd interrupt control registers is configured correctly, the cmpin pin is enabled to wake up mcu from power-down state. brtcklo : when set, p1.0 is enabled to be the clock output of baud-rate timer (brt). the clock rate is brg overflow rate divided by 2. t1cklo : when set, p3.5 is enabled to be the clock output of timer 1. the clock rate is timer 1overflow rate divided by 2. t0cklo : when set, p3.4 is enabled to be the clock output of timer 0. the clock rate is timer 0overflow rate divided by 2. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 224 www.stcmcu.com 7. registers related with uart1 interrupt : ie, ip and iph ie: interrupt enable rsgister (bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ie a8h name ea elvd eadc es et1 ex1 et0 ex0 ea : disables all interrupts. if ea = 0,no interrupt will be acknowledged. if ea = 1, each interrupt source is individually enabled or disabled by setting or clearing its enable bit. es : serial port 1(uart1) interrupt enable bit. if es = 0, serial port 1(uart1) interrupt will be diabled. if es = 1, serial port 1(uart1) interrupt is enabled. iph: interrupt priority high register (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 iph b7h name ppcah plvdh padch psh pt1h px1h pt0h px0h ip: interrupt priority register (bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ip b8h name ppca plvd padc ps pt1 px1 pt0 px0 psh, ps: serial port 1 (uart1) interrupt priority control bits. if psh=0 and ps=0, uart1 interrupt is assigned lowest priority (priority 0). if psh=0 and ps=1, uart1 interrupt is assigned lower priority (priority 1). if psh=1 and ps=0, uart1 interrupt is assigned higher priority (priority 2). if psh=1 and ps=1, uart1 interrupt is assigned highest priority (priority 3). stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 225 www.stcmcu.com 8.1.2.1 mode 0: 8-bit shift register mode 0, selected by writing 0s into bits sm1 and sm0 of scon, puts the serial port into 8-bit shift register mode. serial data enters and exits through rxd. txd outputs the shift clock. eight data bits are transmitted/received with the least-significant (lsb) first. the baud rate is fixed at 1/12 the system clock cycle in the default state. if auxr.5(uart_m0x6) is set, the baud rate is 1/2 system clock cycle. transmission is initiated by any instruction that uses sbuf as a destination register. the write to sbuf signal also loads a 1 into the 9 th position of the transmit shift register and tells the tx control block to commence a transmission. the internal timing is such that one full system clock cycle will elapse between "write to sbuf," and activation of send. send transfers the output of the shift register to the alternate output function line of p3.0, and also transfers shift clock to the alternate output function line of p3.1. at the falling edge of the shift clock, the contents of the shift register are shifted one position to the right. as data bits shift out to the right, 0 come in from the left. when the msb of the data byte is at the output position of the shift register, then the 1 that was initially loaded into the 9 th position is just to the left of the msb, and all positions to the left of that contains zeroes. this condition flags the tx control block to do one last shift and then deactivate send and set ti. both of these actions occur after "write to sbuf". reception is initiated by the condition ren=1 and ri=0. after that, the rx control unit writes the bits 11111110 to the receive shift register, and in the next clock phase activates receive. receive enables shift clock to the alternate output function line of p3.1.at receive is active, the contents of the receive shift register are shifted to the left one position. the value that comes in from the right is the value that was sampled at the p3.0 pin the rising edge of shift clock. as data bits come in from the right, 1s shift out to the left. when the 0 that was initially loaded into the right- most position arrives at the left-most position in the shift register, it flags the rx control block to do one last shift and load sbuf. then receive is cleared and ri is set. 8.1.2 uart1 operation modes the serial port 1 (uart) can be operated in 4 different modes which are configured by setting sm0 and sm1 in sfr scon. mode 1, mode 2 and mode 3 are asynchronous communication. in mode 0, uart1 is used as a simple shift register. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 226 www.stcmcu.com internal bus sbuf zero detector tx control shift send start tx clock ti d s q cl rx control shift receive start rx clock ri input shift reg . sbuf internal bus write to sbuf shift rxd/p3.0 output function 1 1 1 1 1 1 1 0 shift clock txd/p3.1 output function shift rxd/p3.0 input function load sbuf read sbuf serial port interrupt ren ri write to sbuf send shift d1 d0 d2 d3 d4 d5 d6 d7 rxd(data out) txd(shift clock) ti write to scon(clear ri) ri receive shift txd(shift clock) rxd(data in) d0 d1 d2 d3 d4 d5 d6 d7 transmit receive serial port mode 0 sysclk/12 sysclk/2 0 1 auxr.5(uart_m0x6) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 227 www.stcmcu.com 8.1.2.2 mode 1: 8-bit uart with variable baud rate 10 bits are transmitted through txd or received through rxd. the frame data includes a start bit(0), 8 data bits and a stop bit(1). one receive, the stop bit goes into rb8 in sfr C scon. the baud rate is determined by the timer 1 or brt overflow rate. baud rate in mode 1 = (2 smod /32 ) x timer 1 overflow rate ( if auxr.0/s1brs=0 ) = (2 smod /32 ) x brt overflow rate ( if auxr.0/s1brs=1 ) when t1x12=0, timer 1 overflow rate = sysclk/12/(256-th1); when t1x12=1, timer 1 overflow rate = sysclk / (256-th1); and when brtx12=0, dedicated baud-rate timer (brt) overflow rate = sysclk/12/(256-brt); when brtx12=1, dedicated baud-rate timer (brt) overflow rate = sysclk / (256-brt); transmission is initiated by any instruction that uses sbuf as a destination register. the write to sbuf signal also loads a 1 into the 9 th bit position of the transmit shift register and flags the tx control unit that a transmission is requested. transmission actually happens at the next rollover of divided-by-16 counter. thus the bit times are synchronized to the divided-by-16 counter, not to the write to sbuf signal. the transmission begins with activation of send , which puts the start bit at txd. one bit time later, data is activated, which enables the output bit of the transmit shift register to txd. the first shift pulse occurs one bit time after that. as data bits shift out to the right, zeroes are clocked in from the left. when the msb of the data byte is at the output position of the shift register, then the 1 that was initially loaded into the 9 th position is just to the left of the msb, and all positions to the left of that contain zeroes. this condition flags the tx control unit to do one last shift and then deactivate send and set ti. this occurs at the 10 th divide-by-16 rollover after write to sbuf. reception is initiated by a 1-to-0 transition detected at rxd. for this purpose, rxd is sampled at a rate of 16 times the established baud rate. when a transition is detected, the divided-by-16 counter is immediately reset, and 1ffh is written into the input shift register. resetting the divided-by-16 counter aligns its roll-overs with the boundaries of the incoming bit times. the 16 states of the counter divide each bit time into 16ths. at the 7 th , 8 th and 9 th counter states of each bit time, the bit detector samples the value of rxd. the value accepted is the value that was seen in at least 2 of the 3 samples. this is done to reject noise. in order to reject false bits, if the value accepted during the first bit time is not a 0, the receive circuits are reset and the unit continues looking for another 1-to-0 transition. this is to provide rejection of false start bits. if the start bit is valid, it is shifted into the input shift register, and reception of the rest of the frame proceeds. as data bits come in from the right, 1s shift out to the left. when the start bit arrives at the left most position in the shift register,(which is a 9-bit register in mode 1), it flags the rx control block to do one last shift, load sbuf and rb8, and set ri. the signal to load sbuf and rb8 and to set ri is generated if, and only if, the following conditions are met at the time the final shift pulse is generated. 1) ri=0 and 2) either sm2=0, or the received stop bit = 1 if either of these two conditions is not met, the received frame is irretrievably lost. if both conditions are met, the stop bit goes into rb8, the 8 data bits go into sbuf, and ri is activated. at this time, whether or not the above conditions are met, the unit continues looking for a 1-to-0 transition in rxd. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 228 www.stcmcu.com internal bus sbuf zero detector tx control shift start tx clock ti d s q cl rx control shift load sbuf start rx clock ri input shift reg . (9 bits) sbuf internal bus write to sbuf 1ffh 16 shift load sbuf read sbuf serial port interrupt send data tb8 txd 16 1-to-0 transition detector sample bit detector rxd 2 smod =0 smod =1 timer 1 or brt overflow write to sbuf send shift data d1 d0 txd d2 d3 d4 d5 d6 d7 start bit stop bit ti transmit tx clock d1 d0 rxd d2 d3 d4 d5 d6 d7 start bit stop bit rx clock shift bit detector sample times ri receive serial port mode 1 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 229 www.stcmcu.com 8.1.2.3 mode 2: 9-bit uart with fixed baud rate 11 bits are transmitted through txd or received through rxd. the frame data includes a start bit(0), 8 data bits, a programmable 9th data bit and a stop bit(1). on transmit, the 9th data bit comes from tb8 in scon. on receive, the 9th data bit goes into rb8 in scon. the baud rate is programmable to either 1/32 or 1/64 the system clock cycle. baud rate in mode 2 = (2 smod /64) x sysclk transmission is initiated by any instruction that uses sbuf as a destination register. the write to sbuf signal also loads tb8 into the 9 th bit position of the transmit shift register and flags the tx control unit that a transmission is requested. transmission actually happens at the next rollover of divided-by-16 counter. thus the bit times are synchronized to the divided-by-16 counter, not to the write to sbuf signal. the transmission begins when /send is activated, which puts the start bit at txd. one bit time later, data is activated, which enables the output bit of the transmit shift register to txd. the first shift pulse occurs one bit time after that. the first shift clocks a 1(the stop bit) into the 9 th bit position on the shift register. thereafter, only 0s are clocked in. as data bits shift out to the right, 0s are clocked in from the left. when tb8 of the data byte is at the output position of the shift register, then the stop bit is just to the left of tb8, and all positions to the left of that contains 0s. this condition flags the tx control unit to do one last shift, then deactivate /send and set ti. this occurs at the 11 th divided-by-16 rollover after write to sbuf. reception is initiated by a 1-to-0 transition detected at rxd. for this purpose, rxd is sampled at a rate of 16 times whatever baud rate has been estabished. when a transition is detected, the divided-by-16 counter is immediately reset, and 1ffh is written into the input shift register. at the 7 th , 8 th and 9 th counter states of each bit time, the bit detector samples the value of rxd. the value accepted is the value that was seen in at least 2 of the 3 samples. this is done to reject noise. in order to reject false bits, if the value accepted during the first bit time is not a 0, the receive circuits are reset and the unit continues looking for another 1-to-0 transition. if the start bit is valid, it is shifted into the input shift register, and reception of the rest of the frame proceeds. as data bits come in from the right, 1s shift out to the left. when the start bit arrives at the leftmost position in the shift register,(which is a 9-bit register in mode-2 and 3), it flags the rx control block to do one last shift, load sbuf and rb8, and set ri. the signal to load sbuf and rb8 and to set ri is generated if, and only if, the following conditions are met at the time the final shift pulse is generated.: 1) ri=0 and 2) either sm2=0, or the received 9 th data bit = 1 if either of these two conditions is not met, the received frame is irretrievably lost. if both conditions are met, the stop bit goes into rb8, the first 8 data bits go into sbuf, and ri is activated. at this time, whether or not the above conditions are met, the unit continues looking for a 1-to-0 transition at the rxd input. note that the value of received stop bit is irrelevant to sbuf, rb8 or ri. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 230 www.stcmcu.com internal bus sbuf zero detector tx control shift start tx clock ti d s q cl rx control shift load sbuf start rx clock ri input shift reg . (9 bits) sbuf internal bus write to sbuf 1ffh 16 shift load sbuf read sbuf serial port interrupt send data tb8 txd 16 1-to-0 transition detector sample bit detector rxd stop bit gen. 2 smod=0 smod=1 mode 2 (smod is pcon.7) sysclk/2 write to sbuf send shift data d1 d0 txd d2 d3 d4 d5 d6 d7 start bit stop bit ti transmit tx clock d1 d0 rxd d2 d3 d4 d5 d6 d7 start bit stop bit rx clock bit detector sample times receive stop bit gen tb8 rb8 shift ri serial port mode 2 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 231 www.stcmcu.com 8.1.2.4 mode3: 9-bit uart with variable baud rate mode 3 is the same as mode 2 except the baud rate is variable. baud rate in mode 3 = (2 smod /32 ) x timer 1 overflow rate ( if auxr.0/s1brs=0 ) = (2 smod /32 ) x brt overflow rate ( if auxr.0/s1brs=1 ) when t1x12=0, timer 1 overflow rate = sysclk/12/(256-th1); when t1x12=1, timer 1 overflow rate = sysclk / (256-th1); and when brtx12=0, dedicated baud-rate timer (brt) overflow rate = sysclk/12/(256-brt); when brtx12=1, dedicated baud-rate timer (brt) overflow rate = sysclk / (256-brt); in all four modes, transmission is initiated by any instruction that use sbuf as a destination register. reception is initiated in mode 0 by the condition ri = 0 and ren = 1. reception is initiated in the other modes by the incoming start bit with 1-to-0 transition if ren=1. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 232 www.stcmcu.com internal bus sbuf zero detector tx control shift start tx clock ti d s q cl rx control shift load sbuf start rx clock ri input shift reg . (9 bits) sbuf internal bus write to sbuf 1ffh 16 shift load sbuf read sbuf serial port interrupt send data tb8 txd 16 1-to-0 transition detector sample bit detector rxd 2 smod =0 smod =1 timer 1 or brt overflow write to sbuf send shift data d1 d0 txd d2 d3 d4 d5 d6 d7 start bit stop bit ti transmit tx clock d1 d0 rxd d2 d3 d4 d5 d6 d7 start bit stop bit rx clock 16 reset bit detector sample times receive stop bit gen tb8 rb8 shift ri serial port mode 3 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 233 www.stcmcu.com 8.1.3 frame error detection when used for frame error detect, the uart looks for missing stop bits in the communication. a missing bit will set the fe bit in the scon register. the fe bit shares the scon.7 bit with sm0 and the function of scon.7 is determined by pcon.6(smod0). if smod0 is set then scon.7 functions as fe. scon.7 functions as sm0 when smod0 is cleared.when used as fe,scon.7 can only be cleared by software.refer to the following figure. 8.1.4 multiprocessor communications modes 2 and 3 have a special provision for multiproceasor communications. in these modes 9 data bits are received.the 9th one goes into rb8. then comes a stop bit. the port can be programmed such that when the stop bit is received,the serial port interrupt will be activated only if rb8 = 1. this feature is enabled by setting bit sm2 in scon. a way to use this feature in multiprocessor systems is as follows. when the master processor wants to transmit a block of data to one of several slaves, it first sends out an address byte which identifies the target slave.an address byte differs from a data byte in that the 9th bit is 1 in an address byte and 0 in a data byte.with sm2 = 1, no slave will be interrupted by a data byte. an address byte, however,will interrupt all slaves, so that each slave can examine the received byte and see if it is being addressed.the addressed slave will clear its sm2 bit and prepare to receive the data bytes that will be coming. the slaves that werent be - ing addressed leave their sm2s set and go on about their business, ignoring the coming data bytes. sm2 has no effect in mode 0,and in mode 1 can be used to check the validity of the stop bit. in a mode 1 recep - tion, if sm2 = 1, the receive interrupt will not be activated unless a vatid stop bit is received. d1 d0 d2 d3 d4 d5 d6 d7 start bit stop bit d8 sm0/fe sm1 sm2 ren tb8 rb8 ti ri 9-bit data set fe bit if stop=0 sm0 to uart mode control pcon.smod0 scon uart frame error detection stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 234 www.stcmcu.com 8.1.5 automatic address recognition automatic address recognition is a future which allows the uart to recognize certain addresses in the serial bit stream by using hardware to make the comparisons. this feature saves a great deal of software overhead by eliminating the need for the software to examine every serial address which passes by the serial port. this feature is enabled by setting the sm2 bit in scon. in the 9-bit uart modes, mode 2 and mode 3, the receive interrupt flag(ri) will be automatically set when the received byte contains either the given address or the broadcast address. the 9-bit mode requires that the 9 th information bit is a 1 to indicate that the received information is an address and not data. the 8-bit mode is called mode 1. in this mode the ri flag will be set if sm2 is enabled and the information received has a valid stop bit following the 8 address bits and the information is either a given or broadcast address. mode 0 is the shift register mode and sm2 is ignored. using the automatic address recognition feature allows a master to selectively communicate with one or more slaves by invoking the given slave address or addresses. all of the slaves may be contacted by using the broadcast address. two special function registers are used to define the slaves address, saddr, and the address mask, saden. saden is used to define which bits in the saddr are to be used and which bits are dont care. the saden mask can be logically anded with the saddr to create the given address which the master will use for addressing each of the slaves. use of the given address allows multiple slaves to be recognized which excluding others. the following examples will help to show the versatility of this scheme : slave 0 saddr = 1100 0000 saden = 1111 1101 given = 1100 00x0 slave 1 saddr = 1100 0000 saden = 1111 1110 given = 1100 000x in the previous example saddr is the same and the saden data is used to differentiate between the two slaves. slave 0 requires a 0 in bit 0 and it ignores bit 1. slave 1 requires a 0 in bit 1 and bit 0 is ignored. a unique address for slave 0 would be 11000010 since slave 1 requires a 0 in bit 1. a unique address for slave 1 would be 11000001 since a 1 in bit 0 will exclude slave 0. both slaves can be selected at the same time by an address which has bit 0=0 (for slave 0) and bit 1 =0 (for salve 1). thus, both could be addressed with 11000000. in a more complex system the following could be used to select slaves 1 and 2 while excluding slave 0: slave 0 saddr = 1100 0000 saden = 1111 1001 given = 1100 0xx0 slave 1 saddr = 1110 0000 saden = 1111 1010 given = 1110 0x0x slave 2 saddr = 1110 0000 saden = 1111 1100 given = 1110 00xx stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 235 www.stcmcu.com in the above example the differentiation among the 3 slaves is in the lower 3 address bits.slave 0 requires that bit0 = 0 and it can be uniquely addressed by 11100110. slave 1 requires that bit 1=0 and it can be uniquely addressed by 11100101. slave 2 requires that bit 2=0 and its unique address is 11100011. to select salve 0 and 1 and exclude slave 2, use address 11100100, since it is necessary to make bit2=1 to exclude slave 2. the broadcast address for each slave is created by taking the logic or of saddr and saden. zeros in this result are trended as dont cares. in most cares, interpreting the dont cares as ones, the broadcast address will be ff hexadecimal. upon reset saddr and saden are loaded with 0s. this produces a given address of all dont cares as well as a broadcast address of all dont cares. this effectively disables the automatic addressing mode and allows the microcontroller to use standard 80c51-type uart drivers which do not make use of this feature. example: write an program that continually transmits characters from a transmit buffer. if incoming characters are detected on the serial port, store them in the receive buffer starting at internal ram location 50h. assume that the stc12c5a60s2 series mcu serial port has already been initialized in mode 1. solution: org 0030h mov r0, #30h ;pointer for tx buffer mov r1, #50h ;pointer for rx buffer loop: jb ri, receive ;character received? ;yes: process it jb ti, tx ;previous character transmitted ? ;yes: process it sjmp loop ;no: continue checking tx: mov a, @r0 ;get character from tx buffer mov c, p ;put parity bit in c cpl c ;change to odd parity mov acc.7, c ;add to character code clr ti ;clear transmit flag mov sbuf, a ;send character clr acc.7 ;strip off parity bit inc r0 ;point to next character in buffer cjne r0, #50h, loop ;end of buffer? ;no: continue mov r0, #30h ;yes: recycle sjmp loop ;continue checking rx: clr ri ;clear receive flag mov a, sbuf ;read character into a mov c, p ;for odd parity in a, p should be set cpl c ;complementing correctly indicates "error" clr acc.7 ;strip off parity mov @r1, a ;store received character in buffer inc r1 ;point to next location in buffer sjmp loop ;continue checking end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 236 www.stcmcu.com when timer 1 is used as the baud rate generator, the timer 1 interrupt should be disabled in this application. the timer itself can be configured for either timer or cormter operation, and in any of its 3 running modes. in the most typcial applications, it is configured for timer operation, in the auto-reload mode (high nibble of tmod = 0010b). one can achieve very low baud rate with timer 1 by leaving the timer 1 interrupt enabled, and configuring the timer to run as a 16-bit timer (high nibble of tmod = 0001b), and using the timer 1 interrupt to do a l6-bit software reload. the following figure lists various commonly used baud rates and how they can be obtained from timer 1. baud rate f osc smod timer 1 c/t mode reload value mode 0 max:1mhz mode 2 max:375k mode 1,3:62.5k 19.2k 9.6k 4.8k 2.4k 1.2k 137.5 110 110 12mhz 12mhz 12mhz 11.059mhz 11.059mhz 11.059mhz 11.059mhz 11.059mhz 11.986mhz 6mhz 12mhz x 1 1 1 0 0 0 0 0 0 0 x x 0 0 0 0 0 0 0 0 0 x x 2 2 2 2 2 2 2 2 1 x x ffh fdh fdh fah f4h e8h 1dh 72h feebh timer 1 generated commonly used baud rates 8.1.6 buad rates and demo program the baud rate in mode 0 is fixed: sysclk 12 mode 0 baud rate = when auxr.5/uart_m0x6 =0 sysclk 2 or = when auxr.5/uart_m0x6 =1 the baud rate in mode 2 depends on the value of bit smod in special function register pcon. if smod =0 (which is the value on reset), the baud rate 1 / 64 the system clock cycle. if smod = 1, the baud rate is 1 / 32 the system clock cycle . 2 smod 64 mode 2 baud rate = (sysclk) (sysclk) in the stc12c5a60s2, the baud rates in modes 1 and 3 are determined by timer1 or brt overflow rate. the baud rate in mode 1 and 3 are fixed: mode 1,3 baud rate = (2 smod /32 ) x timer 1 overflow rate ( if auxr.0/s1brs=0 ) = (2 smod /32 ) x brt overflow rate ( if auxr.0/s1brs=1 ) timer 1 overflow rate = (sysclk/12)/(256 - th1); brt overflow rate = (sysclk/2) / (256 Cbrt), when auxr.2/brtx12=1 or = (sysclk/2/12) / (256 C brt) , when auxr.2/brtx12=0 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 237 www.stcmcu.com the following program is an example that domestrates uart communication with independent baud rate generator. ;/*----------------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited -------------------------------------------*/ ;/* --- stc 1t series mcu uart communication demo ---------------------*/ ;/* --- mobile: (86)13922809991 ---------------------------------------------------*/ ;/* --- fax: 86-755-82905966 -------------------------------------------------------*/ ;/* --- tel: 86-755-82948412 --------------------------------------------------------*/ ;/* --- web: www.stcmcu.com ---------------------------------------------------*/ ;/* if you want to use the program or the program referenced in the */ ;/* article, please specify in which data and procedures from stc */ ;/*-----------------------------------------------------------------------------------------*/ #include #include sfr auxr = 0x8e; sfr auxr1 = 0xa2; sfr brt = 0x9c; sbit mcu_start_led = p1^4; //unsigned char array[9] = {0,2,4,6,8,10,12,14,16}; unsigned char array[9] = {0x00, 0x02, 0x04, 0x06, 0x08, 0x0a, 0x0c,0x0e, 0x10}; #define reload_count 0xfb //18.432mhz, 12t, smod=0, 9600bps void serial_port_initial( ); void send_uart(unsigned char); void uart_interrupt_receive(void); void delay (void); void display_mcu_start_led (void); void main(void) { unsigned char i = 0; serial_port_initial( ); //initialize serial port display_mcu_start_led( ); //open led indicator, mcu start-up send_uart (0x34); //uart send data send_uart(0xa7); for(i=0; i<9; i++) { send_uart(array[i]); } while(1); } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 238 www.stcmcu.com /* void serial_port_initial( ) //use timer 1 for baud rate generator { scon = 0x50; //0101,0000 8-bit variable baud rate, no odd parity bit tmod = 0x21; //0011,0001 use timer 1 for 8-bit auto-reload counter th1 = reload_count; //set timer 1 auto-reload value tl1 = reload_count; tr1 = 1; //start timer 1 es = 1; //enable serial port interrupt ea = 1; //set global enable bit } */ void serial_port_initial( ) //use independent baud rate generator for baud rate generator { scon = 0x50; //0101,0000 8-bit variable baud rate, no odd parity bit brt = reload_count; auxr = 0x11; //t0x12, t1x12, uart_m0x6, brtr, brtx12, xram, s1brs //baud = sysclk / (256-reload_count)/32/12 (12t mode) //baud = sysclk / (256-reload_count)/32 (1t mode) //baud = 1, start independent baud rate generator //s1brs = 1, uart use independent baud rate generator for baud rate generator //timer 1 can be released to timer , counter or clock-output //auxr =0x80; //if enable this instruction, serial port would be p1 port rather than p3 es = 1; //enable serial port interrupt ea = 1; //set global enable bit } void send_uart(unsigned char i) { es = 0; //close serial port interrupt ti = 0; //clear uart transmit interrupt flag sbuf = i; while (ti == 0); //wait to finish transmit ti = 0; //clear uart transmit interrupt flag es = 1; //enable serial port interrupt } void uart_interrupt_receive (void) interrupt 4 { unsigned char k = 0; if (ri == 1) { ri = 0; k = sbuf; send_uart (k+1); } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 239 www.stcmcu.com else { ti = 0; } } void delay (void) { unsigned int j = 0; unsigned int g = 0; for (j=0; j<5; j++) { for (g=0; g<50000; g++) { _nop_( ); _nop_( ); _nop_( ); } } } void display_mcu_start_led (void) { unsigned char i = 0; for (i=0; i<5; i++) { mcu_start_led = 0; //open mcu-start-led indicator delay( ); mcu_start_led = 1; //close mcu-start-led indicator delay( ); mcu_start_led = 0; //open mcu-start-led indicator } } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 240 www.stcmcu.com 8.1.7 demo programs about uart1 (c and asm) 1. c program: /*-------------------------------------------------------------------------------*/ /* --- stc mcu international limited -----------------------------------*/ /* --- stc12c5axx series mcu uart (8-bit/9-bit)demo ----------*/ /* --- mobile: (86)13922809991 ------------------------------------------*/ /* --- fax: 86-755-82905966 ----------------------------------------------*/ /* --- tel: 86-755-82948412 -----------------------------------------------*/ /* --- web: www.stcmcu.com -----------------------------------------*/ /* if you want to use the program or the program referenced in the */ /* article, please specify in which data and procedures from stc */ /*-------------------------------------------------------------------------------*/ #include "reg51.h" #include "intrins.h" typedef unsigned char byte; typedef unsigned int word; #define fosc 18432000l //system frequency #define baud 9600 //uart baudrate /*define uart parity mode*/ #define none_parity 0 //none parity #define odd_parity 1 //odd parity #define even_parity 2 //even parity #define mark_parity 3 //mark parity #define space_parity 4 //space parity #define paritybit even_parity //testing even parity sbit bit9 = p2^2; //p2.2 show uart data bit9 bit busy; void senddata(byte dat); void sendstring(char *s); void main() { #if (paritybit == none_parity) scon = 0x50; //8-bit variable uart #elif (paritybit == odd_parity) || (paritybit == even_parity) || (paritybit == mark_parity) scon = 0xda; //9-bit variable uart, parity bit initial to 1 #elif (paritybit == space_parity) scon = 0xd5; //9-bit variable uart, parity bit initial to 0 #endif stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 241 www.stcmcu.com tmod = 0x20; //set timer1 as 8-bit auto reload mode th1 = tl1 = -(fosc/12/32/baud); //set auto-reload vaule tr1 = 1; //timer1 start run es = 1; //enable uart interrupt ea = 1; //open master interrupt switch sendstring("stc12c5a60s2\r\nuart test !\r\n"); while(1); } /*---------------------------- uart interrupt service routine ----------------------------*/ void uart_isr() interrupt 4 using 1 { if (ri) { ri = 0; //clear receive interrupt flag p0 = sbuf; //p0 show uart data bit9 = rb8; //p2.2 show parity bit } if (ti) { ti = 0; //clear transmit interrupt flag busy = 0; //clear transmit busy flag } } /*---------------------------- send a byte data to uart input: dat (data to be sent) output:none ----------------------------*/ void senddata(byte dat) { while (busy); //wait for the completion of the previous data is sent acc = dat; //calculate the even parity bit p (psw.0) if (p) //set the parity bit according to p { #if (paritybit == odd_parity) tb8 = 0; //set parity bit to 0 #elif (paritybit == even_parity) tb8 = 1; //set parity bit to 1 #endif } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 242 www.stcmcu.com else { #if (paritybit == odd_parity) tb8 = 1; //set parity bit to 1 #elif (paritybit == even_parity) tb8 = 0; //set parity bit to 0 #endif } busy = 1; sbuf = acc; //send data to uart buffer } /*---------------------------- send a string to uart input: s (address of string) output:none ----------------------------*/ void sendstring(char *s) { while (*s) //check the end of the string { senddata(*s++); //send current char and increment string ptr } } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 243 www.stcmcu.com 2. assembly program: /*-------------------------------------------------------------------------------*/ /* --- stc mcu international limited -----------------------------------*/ /* --- stc12c5axx series mcu uart (8-bit/9-bit)demo ----------*/ /* --- mobile: (86)13922809991 ------------------------------------------*/ /* --- fax: 86-755-82905966 ----------------------------------------------*/ /* --- tel: 86-755-82948412 -----------------------------------------------*/ /* --- web: www.stcmcu.com -----------------------------------------*/ /* if you want to use the program or the program referenced in the */ /* article, please specify in which data and procedures from stc */ /*-------------------------------------------------------------------------------*/ ;/*define uart parity mode*/ #define none_parity 0 //none parity #define odd_parity 1 //odd parity #define even_parity 2 //even parity #define mark_parity 3 //mark parity #define space_parity 4 //space parity #define paritybit even_parity //testing even parity ;----------------------------------------- busy bit 20h.0 ;transmit busy flag ;----------------------------------------- org 0000h ljmp main org 0023h ljmp uart_isr ;----------------------------------------- org 0100h main: clr busy clr ea mov sp, #3fh #if (paritybit == none_parity) mov scon, #50h ;8-bit variable uart #elif (paritybit == odd_parity) || (paritybit == even_parity) || (paritybit == mark_parity) mov scon, #0dah ;9-bit variable uart, parity bit initial to 1 #elif (paritybit == space_parity) mov scon, #0d5h ;9-bit variable uart, parity bit initial to 0 #endif ;------------------------------- stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 244 www.stcmcu.com mov tmod, #20h ;set timer1 as 8-bit auto reload mode mov a, #0fbh ;256-18432000/12/32/9600 mov th1, a ;set auto-reload vaule mov tl1, a setb tr1 ;timer1 start run setb es ;enable uart interrupt setb ea ;open master interrupt switch ;------------------------------- mov dptr, #teststr ;load string address to dptr lcall sendstring ;send string ;------------------------------- sjmp $ ;----------------------------------------- teststr: ;test string db "stc12c5a60s2 uart test !", 0dh,0ah,0 ;/*---------------------------- ;uart2 interrupt service routine ;----------------------------*/ uart_isr: push acc push psw jnb ri, checkti ;check ri bit clr ri ;clear ri bit mov p0, sbuf ;p0 show uart data mov c, rb8 mov p2.2, c ;p2.2 show parity bit checkti: jnb ti, isr_exit ;check s2ti bit clr ti ;clear s2ti bit clr busy ;clear transmit busy flag isr_exit: pop psw pop acc reti ;/*---------------------------- ;send a byte data to uart ;input: acc (data to be sent) ;output:none ;----------------------------*/ senddata: jb busy, $ ;wait for the completion of the previous data is sent mov acc, a ;calculate the even parity bit p (psw.0) jnb p, even1inacc ;set the parity bit according to p stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 245 www.stcmcu.com odd1inacc: #if (paritybit == odd_parity) clr tb8 ;set parity bit to 0 #elif (paritybit == even_parity) setb tb8 ;set parity bit to 1 #endif sjmp paritybitok even1inacc: #if (paritybit == odd_parity) setb tb8 ;set parity bit to 1 #elif (paritybit == even_parity) clr tb8 ;set parity bit to 0 #endif paritybitok: ;parity bit set completed setb busy mov sbuf, a ;send data to uart buffer ret ;/*---------------------------- ;send a string to uart ;input: dptr (address of string) ;output:none ;----------------------------*/ sendstring: clr a movc a, @a+dptr ;get current char jz stringend ;check the end of the string inc dptr ;increment string ptr lcall senddata ;send current char sjmp sendstring ;check next stringend: ret ;----------------------------------------- end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 246 www.stcmcu.com 8.2 secondary uart (s2) s2 is the secondary uart of stc12c5a60s2 that its function is fully the same with the major uart described in last section and only with the exception that no enhanced function included. an additional baud-rate generator (brt) is available in s2 to simplify the baud-rate generation and release timer1 for use in other purposes. the additional baud-rate generator can also be configured to provide a programmable clock output on p1.0. combined with timer1 and timer0, stc12c5a60s2 will be able to provide three individual programmable clock outputs on three general-purpose i/o pins, respectively. 8.2.1 special function registers about s2 (uart2) symbol description address bit address and symbol msb lsb value after power-on or reset s2con s2 control 9ah s2sm0 s2sm1 s2sm2 s2ren s2tb8 s2rb8 s2ti s2ri 0000 0000b s2sbuf s2 serial buffer 9bh xxxx xxxxb brt baud-rate timer 9ch 0000 0000b auxr auxiliary register 8eh t0x12 t1x12 uart_m0x6 brtr s2smod brtx12 extram s1brs 0000 0000b ie interrupt enable a8h ea elvd eadc es et1 ex1 et0 ex0 0x00 0000b ie2 interrupt enable 2 afh - - - - - - espi es2 xxxx xx00b ip2 2rd interrupt priority low register b5h - - - - - - pspi ps2 xxxx xx00b ip2h 2rd interrupt priority low register b6h - - - - - - pspih ps2h xxxx xx00b auxr1 auxiliary register1 a2h - pca_p4 spi_p4 s2_p4 gf2 adrj - dps 0000 0000b there are several special function registers which should be understood by users before using the secondary uart. 1. serial port 2 control register: s2con (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 s2con 9ah name s2sm0 s2sm1 s2sm2 s2ren s2tb8 s2rb8 s2ti s2ri s2sm0,s2sm1 : serial port 2 mode select bit 0/1. s2sm0 s2sm1 operation modes description baud rate 0 0 mode 0 8-bit shift register sysclk/12 0 1 mode 1 8-bit uart, baud-rate variable (2 s2smod /32) x (brt overflow rate) 1 0 mode 2 9-bit uart, baud-rate fixed (2 s2smod /64) x sysclk 1 1 mode 3 9-bit uart, baud-rate variable (2 s2smod /32) x (brt overflow rate) if brtx12 = 0, brt overflow rate = sysclk / 12 / (256-brt); if brtx12 = 1, brt overflow rate = sysclk / (256-brt); stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 247 www.stcmcu.com s2sm2 : enable the automatic address recognition feature in mode 2 and 3. if s2sm2=1,s2 ri will not be set unless the received 9th data bit is 1, indicating an address, and the received byte is a given or broadcast address. in mode1, if s2sm2=1 then ri will not be set unless a valid stop bit was received, and the received byte is a given or broadcast address. s2ren : enable the serial port reception.when set, enable serial reception. when clear, disable the secondary serial port reception. s2tb8 : the 9th data bit which will be transmitted in mode 2 and 3. s2rb8 : in mode 2 and 3, the received 9th data bit will go into this bit. s2ti : transmit interrupt flag. after a transmitting has been finished, the hardware will set this bit. s2ri : receive interrupt flag. after reception has been finished, the hardware will set this bit. 2. serial port 2 data buffer register: s2buf sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 s2buf 9bh name it is used as the buffer register in transmission and reception. this sfr accesses two registers; a transmit shift reg - ister and a receive latch register. when data is written to s2buf, it goes to the transmit shift register and is held for serial transmission. writing a byte to s2buf initiates the transmission. a read of s2buf returns the contents of the receive latch. 3. brt: dedicated baud-rate timer register (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 brt 9ch name it is used as the reload register for generating the baud-rate of the uart. 4. auxr: auxiliary register (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 auxr 8eh name t0x12 t1x12 uart_m0x6 brtr s2smod brtx12 extram s1brs t0x12 : timer 0 clock source bit. 0 : the clock source of timer 0 is sysclk/12. it will compatible to the traditional 80c51 mcu 1 : the clock source of timer 0 is sysclk/1. it will drive the t0 faster than a traditional 80c51 mcu t1x12 : timer 1 clock source bit. 0 : the clock source of timer 1 is sysclk/12. it will compatible to the traditional 80c51 mcu 1 : the clock source of timer 1 is sysclk/1. it will drive the t0 faster than a traditional 80c51 mcu uart_m0x6 : baud rate select bit of uart1 while it is working under mode-0 0 : the baud-rate of uart in mode 0 is sysclk/12. 1 : the baud-rate of uart in mode 0 is sysclk/2. brtr : dedicated baud-rate timer run control bit. 0 : the baud-rate generator is stopped. 1 : the baud-rate generator is enabled. s2smod : the baud-rate of uart2 double contol bit. 0 : default. the baud-rate of uart2 (s2) is not doubled. 1 : the baud-rate uart2 (s2) is doubled. brtx12 : dedicated baud-rate timer counter control bit. 0 : the baud-rate generator is incremented every 12 system clocks. 1 : the baud-rate generator is incremented every system clock. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 248 www.stcmcu.com extram : internal / external ram access control bit. 0 : on-chip auxiliary ram is enabled and located at the address 0x0000 to 0x03ff. for address over 0x03ff, off-chip expanded ram becomes the target automatically. 1 : on-chip auxiliary ram is always disabled. s1brs : the baud-rate generator of uart1 select bit. 0 : default. select timer 1 as the baud-rate generator of uart1 1 : timer 1 is replaced by the independent baud-rate generator which is selected as the baud-rate of uart. in other words, timer 1 is released to use in other functions. for stc12c5a60s2 series, secondary uart (s2) only can select dedicated baud-rate timer as its baud-rate generator. while uart1 not only can select dedicated baud-rate timer, but also can select timer 1 as its baud- rate generator. 5. registers related with uart2 (s2) interrupt : ie, ie2, ip2 and ip2h ie: interrupt enable rsgister (bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ie a8h name ea elvd eadc es et1 ex1 et0 ex0 ea : disables all interrupts. if ea = 0,no interrupt will be acknowledged. if ea = 1, each interrupt source is individually enabled or disabled by setting or clearing its enable bit. ie2: interrupt enable 2 rsgister (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ie2 afh name - - - - - - espi es2 es2 : serial port 2 (uart2) interrupt enable bit. if es2 = 0, uart2 interrupt will be diabled. if es2 = 1, uart2 interrupt is enabled. ip2h: interrupt priority high register (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ip2h b6h name - - - - - - pspih ps2h ip2: interrupt priority register (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ip2 b5h name - - - - - - pspi ps2 pspih, pspi: spi interrupt priority control bits. if pspih=0 and pspi=0, spi interrupt is assigned lowest priority (priority 0). if pspih=0 and pspi=1, spi interrupt is assigned lower priority (priority 1). if pspih=1 and pspi=0, spi interrupt is assigned higher priority (priority 2). if pspih=1 and pspi=1, spi interrupt is assigned highest priority (priority 3). ps2h, ps2 : serial port 2 (uart2) interrupt priority control bits. if ps2h=0 and ps2=0, uart2 interrupt is assigned lowest priority (priority 0). if ps2h=0 and ps2=1, uart2 interrupt is assigned lower priority (priority 1). if ps2h=1 and ps2=0, uart2 interrupt is assigned higher priority (priority 2). if ps2h=1 and ps2=1, uart2 interrupt is assigned highest priority (priority 3). stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 249 www.stcmcu.com 6. auxiliary 1 register: auxr1 register auxr1 is used to select whether pca/pwm/spi/uart2 function is on p1 port or p4 port mnemonic address bit 7 6 5 4 3 2 1 0 auxr1 a2h name - pca_p4 spi_p4 s2_p4 gf2 adrj - dps pca _p4 0 : default. the pca function is on p1[4:2] 1 : the pca function on p1[4:2] is switched to p4[3:1]. eci is switched from p1.2 to p4.1 pca0/pwm0 is switched from p1.3 to p4.2 pca1/pwm1 is switched from p1.4 to p4.3 spi_p4 0 : default. the spi function is on p1[7:4] 1 : the spi function on p1[7:4] is switched to p4[3:0]. sclk is switched from p1.7 to p4.3 mosi is switched from p1.6 to p4.2 miso is switched from p1.5 to p4.1 ss is switched from p1.4 to p4.0 s2_p4 0 : default. the uart2(s2) function is on p1[3:2] 1 : the uart2(s2) function on p1[3:2] is switched to p4[3:2]. txd2 is switched from p1.3 to p4.3 rxd2 is switched from p1.2 to p4.2 gf2 : general flag. it can be used by software. adrj 0 : the 10-bit conversion result of adc is arranged as {adc_res[7:0], adc_resl[1:0]}. 1 : the 10-bit conversion result is right-justified, {adc_res[1:0], adc_resl[7:0]}. dps 0 : default. dptr0 is selected as data pointer. 1 : the secondary dptr is switched to use. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 250 www.stcmcu.com 8.2.3.1 mode 0: 8-bit shift register serial data enters and exits through rxd2/p1.2(rxd2/p4.2). txd2/p1.3(txd2/p4.3) outputs the shift clock. eight data bits are transmitted/received with the lsb first. the baud rate is fixed at 1/12 the system clock. regardless of baud-rate generation, the operation in mode 0 for s2 uart is the same as the standard uart in mode 0. baud-rate in mode 0 = sysclk / 12 8.2.3.2 mode 1: 8-bit uart2 with variable baud-rate 10 bits are transmitted through txd2/p1.3(txd2/p4.3) or received through rxd2/p1.2(rxd2/p4.2). the frame data includes a start bit(0), 8 data bits and a stop bit(1). one receive, the stop bit goes into s2rb8 in sfr C s2con. the baud rate is determined by the brt overflow rate. regardless of baud-rate generation, the operation in mode 1 for s2 uart is the same as the standard uart in mode 1. baud rate in mode 1 = (2 s2smod /32 ) x brt timer overflow rate if brtx12 = 0, brt timer overflow = sysclk / 12 / (256-brt) if brtx12 = 1, brt timer overflow = sysclk / (256-brt) 8.2.3.3 mode 2: 9-bit uart2 with fixed baud-rate 11 bits are transmitted through txd2/p1.3(txd2/p4.3) or received through rxd2/p1.2(rxd2/p4.2). the frame data includes a start bit(0), 8 data bits, a programmable 9th bit and a stop bit(1). on transmit, the 9th data bit comes from s2tb8 in s2con. on receive, the 9th data bit goes into s2rb8 in s2con. the baud rate is programmable to either 1/32 or 1/64 the system clock cycle. the operation in mode 2 for s2 uart is the same as the standard uart in mode 2. baud rate in mode 2 = (2 s2smod /64) x sysclk 8.2.3.4 mode 3: 9-bit uart2 with variable baud-rate mode 3 is the same as mode 2 except the baud rate is variable. baud rate in mode 3 = (2 s2smod /32 ) x brt timer overflow rate if brtx12 = 0, brt timer overflow = sysclk / 12 / (256-brt) if brtx12 = 1, brt timer overflow = sysclk / (256-brt) * when s2_p4 bit in auxr1 register is set, the function of uart2 is redirected to p4.2 for rxd2 and p4.3 for txd2. 8.2.3 uart2 operation modes the serial port 2 (uart2) can be operated in 4 different modes which are configured by setting s2sm0 and s2sm1 in sfr s2con. mode 1, mode 2 and mode 3 are asynchronous communication. in mode 0, uart2 is used as a simple shift register. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 251 www.stcmcu.com 8.2.4 demo program about secondary uart 1. demo program 1 c program /*-------------------------------------------------------------------------------*/ /* --- stc mcu international limited ----------------------------------*/ /* --- stc12c5axx series mcu uart2 (8-bit/9-bit)demo --------*/ /* --- mobile: (86)13922809991 ------------------------------------------*/ /* --- fax: 86-755-82905966 ----------------------------------------------*/ /* --- tel: 86-755-82948412 -----------------------------------------------*/ /* --- web: www.stcmcu.com -----------------------------------------*/ /* if you want to use the program or the program referenced in the */ /* article, please specify in which data and procedures from stc */ /*-------------------------------------------------------------------------------*/ #include "reg51.h" #include "intrins.h" typedef unsigned char byte; typedef unsigned int word; #define fosc 18432000l //system frequency #define baud 115200 //uart baudrate /*define uart parity mode*/ #define none_parity 0 //none parity #define odd_parity 1 //odd parity #define even_parity 2 //even parity #define mark_parity 3 //mark parity #define space_parity 4 //space parity #define paritybit even_parity //testing even parity /*declare sfr associated with the uart2 */ sfr auxr = 0x8e; //auxiliary register sfr s2con = 0x9a; //uart2 control register sfr s2buf = 0x9b; //uart2 data buffer sfr brt = 0x9c; //baudrate generator sfr ie2 = 0xaf; //interrupt control 2 #define s2ri 0x01 //s2con.0 #define s2ti 0x02 //s2con.1 #define s2rb8 0x04 //s2con.2 #define s2tb8 0x08 //s2con.3 bit busy; void senddata(byte dat); void sendstring(char *s); stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 252 www.stcmcu.com void main() { #if (paritybit == none_parity) s2con = 0x50; //8-bit variable uart #elif (paritybit == odd_parity) || (paritybit == even_parity) || (paritybit == mark_parity) s2con = 0xda; //9-bit variable uart, parity bit initial to 1 #elif (paritybit == space_parity) s2con = 0xd5; //9-bit variable uart, parity bit initial to 0 #endif brt = -(fosc/32/baud); //set auto-reload vaule of baudrate generator auxr = 0x14; //baudrate generator work in 1t mode ie2 = 0x01; //enable uart2 interrupt ea = 1; //open master interrupt switch sendstring("stc12c5a60s2\r\nuart2 test !\r\n"); while(1); } /*---------------------------- uart2 interrupt service routine ----------------------------*/ void uart2() interrupt 8 using 1 { if (s2con & s2ri) { s2con &= ~s2ri; //clear receive interrupt flag p0 = s2buf; //p0 show uart data p2 = (s2con & s2rb8); //p2.2 show parity bit } if (s2con & s2ti) { s2con &= ~s2ti; //clear transmit interrupt flag busy = 0; //clear transmit busy flag } } /*---------------------------- send a byte data to uart input: dat (data to be sent) output:none ----------------------------*/ stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 253 www.stcmcu.com void senddata(byte dat) { while (busy); //wait for the completion of the previous data is sent acc = dat; //calculate the even parity bit p (psw.0) if (p) //set the parity bit according to p { #if (paritybit == odd_parity) s2con &= ~s2tb8; //set parity bit to 0 #elif (paritybit == even_parity) s2con |= s2tb8; //set parity bit to 1 #endif } else { #if (paritybit == odd_parity) s2con |= s2tb8; //set parity bit to 1 #elif (paritybit == even_parity) s2con &= ~s2tb8; //set parity bit to 0 #endif } busy = 1; s2buf = acc; //send data to uart2 buffer } /*---------------------------- send a string to uart input: s (address of string) output:none ----------------------------*/ void sendstring(char *s) { while (*s) //check the end of the string { senddata(*s++); //send current char and increment string ptr } } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 254 www.stcmcu.com assembly program /*-------------------------------------------------------------------------------*/ /* --- stc mcu international limited ----------------------------------*/ /* --- stc12c5axx series mcu uart2 (8-bit/9-bit) demo -------*/ /* --- mobile: (86)13922809991 ------------------------------------------*/ /* --- fax: 86-755-82905966 ----------------------------------------------*/ /* --- tel: 86-755-82948412 -----------------------------------------------*/ /* --- web: www.stcmcu.com -----------------------------------------*/ /* if you want to use the program or the program referenced in the */ /* article, please specify in which data and procedures from stc */ /*-------------------------------------------------------------------------------*/ ;/*define uart parity mode*/ #define none_parity 0 //none parity #define odd_parity 1 //odd parity #define even_parity 2 //even parity #define mark_parity 3 //mark parity #define space_parity 4 //space parity #define paritybit even_parity //testing even parity ;----------------------------------------- ;/*declare sfr associated with the uart2 */ auxr equ 08eh ;auxiliary register s2con equ 09ah ;uart2 control register s2buf equ 09bh ;uart2 data buffer brt equ 09ch ;baudrate generator ie2 equ 0afh ;interrupt control 2 s2ri equ 01h ;s2con.0 s2ti equ 02h ;s2con.1 s2rb8 equ 04h ;s2con.2 s2tb8 equ 08h ;s2con.3 ;----------------------------------------- busy bit 20h.0 ;transmit busy flag ;----------------------------------------- org 0000h ljmp main org 0043h ljmp uart2_isr ;----------------------------------------- stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 255 www.stcmcu.com org 0100h main: clr busy clr ea mov sp, #3fh #if (paritybit == none_parity) mov s2con, #50h ;8-bit variable uart #elif (paritybit == odd_parity) || (paritybit == even_parity) || (paritybit == mark_parity) mov s2con, #0dah ;9-bit variable uart, parity bit initial to 1 #elif (paritybit == space_parity) mov s2con, #0d5h ;9-bit variable uart, parity bit initial to 0 #endif ;------------------------------- mov brt, #0fbh ;set auto-reload vaule of baudrate generator (256-18432000/32/115200) mov auxr, #14h ;baudrate generator work in 1t mode orl ie2, #01h ;enable uart2 interrupt setb ea ;------------------------------- mov dptr, #teststr ;load string address to dptr lcall sendstring ;send string ;------------------------------- sjmp $ ;----------------------------------------- teststr: ;test string db "stc12c5a60s2 uart2 test !", 0dh,0ah,0 ;/*---------------------------- ;uart2 interrupt service routine ;----------------------------*/ uart2_isr: push acc push psw mov a, s2con ;read uart2 control register jnb acc.0, checkti ;check s2ri bit anl s2con, #not s2ri ;clear s2ri bit mov p0, s2buf ;p0 show uart data anl a, #s2rb8 ;mask s2rb8 mov p2, a ;p2.2 show parity bit checkti: ; mov a, s2con ;read uart2 control register jnb acc.1, isr_exit ;check s2ti bit anl s2con, #not s2ti ;clear s2ti bit clr busy ;clear transmit busy flag isr_exit: pop psw pop acc reti stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 256 www.stcmcu.com ;/*---------------------------- ;send a byte data to uart ;input: acc (data to be sent) ;output:none ;----------------------------*/ senddata: jb busy, $ ;wait for the completion of the previous data is sent mov acc, a ;calculate the even parity bit p (psw.0) jnb p, even1inacc ;set the parity bit according to p odd1inacc: #if (paritybit == odd_parity) anl s2con, #not s2tb8 ;set parity bit to 0 #elif (paritybit == even_parity) orl s2con, #s2tb8 ;set parity bit to 1 #endif sjmp paritybitok even1inacc: #if (paritybit == odd_parity) orl s2con, #s2tb8 ;set parity bit to 1 #elif (paritybit == even_parity) anl s2con, #not s2tb8 ;set parity bit to 0 #endif paritybitok: ;parity bit set completed setb busy mov s2buf, a ;send data to uart2 buffer ret ;/*---------------------------- ;send a string to uart ;input: dptr (address of string) ;output:none ;----------------------------*/ sendstring: clr a movc a, @a+dptr ;get current char jz stringend ;check the end of the string inc dptr ;increment string ptr lcall senddata ;send current char sjmp sendstring ;check next stringend: ret ;----------------------------------------- end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 257 www.stcmcu.com #include #include sfr s2con = 0x9a; //s2sm0, s2sm1, s2sm2, s2ren, s2tb8, s2rb8, s2ti, s2ri sfr ie2 = 0xaf; //x, x, x, x, x, x, espi, es2 sfr s2buf = 0x9b; sfr auxr = 0x8e; sfr brt = 0x9c; sfr iap_contr = 0xc7; sfr ccon = 0xd8; sfr cmod = 0xd9; sfr cl = 0xe9; sfr ch = 0xf9; sfr ccap0l = 0xea; sfr ccap0h = 0xfa; sfr ccapm0 = 0xda; sfr ccapm1 = 0xdb; sfr cr = 0xde; sbit mcu_start_led = p1^7; sbit s2_interrupt_receive_led = p1^4; //unsigned char self_command_array[4] = {0x22, 0x33, 0x44, 0x55}; # define reload_count 0xfb //18.432mhz, 12t, smod=0, 9600bps //# define reload_count 0xf6 //18.432mhz, 12t, smod=0, 4800bps //# define reload_count 0xec //18.432mhz, 12t, smod=0, 2400bps //# define reload_count 0xd8 //18.432mhz, 12t, smod=0, 1200bps void serial_port_one_initial( ); void send_uart_one(unsigned char); void uart_one_interrupt_receive(void); 2. demo program 2 c program /*-------------------------------------------------------------------------------*/ /* --- stc mcu international limited ----------------------------------*/ /* --- stc12c5axx series mcu uart2 communicaton demo ----*/ /* --- mobile: (86)13922809991 ------------------------------------------*/ /* --- fax: 86-755-82905966 ----------------------------------------------*/ /* --- tel: 86-755-82948412 -----------------------------------------------*/ /* --- web: www.stcmcu.com -----------------------------------------*/ /* if you want to use the program or the program referenced in the */ /* article, please specify in which data and procedures from stc */ /*-------------------------------------------------------------------------------*/ stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 258 www.stcmcu.com void serial_port_two_initial( ); void send_uart_two(unsigned char); void uart_two_interrupt_receive(void); void soft_reset_to_isp_monitor(void); void delay(void); void display_mcu_start_led(void); void send_pwm(void); void main(void) { unsigned int array_point = 0; unsigned char xdata test_array_ont[512] = { 0x00, 0x01 0x02, 0x03, 0x04 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf, 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb ,0xcc, 0xcd, 0xce, 0xcf, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7 0xd8, 0xd9, 0xda, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff, stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 259 www.stcmcu.com 0xff, 0xfe, 0xfd, 0xfc, 0xfb, 0xfa, 0xf9, 0xf8, 0xf7, 0xf6, 0xf5, 0xf4, 0xf3, 0xf2, 0xf1, 0xf0, 0xef, 0xee, 0xed, 0xec, 0xeb, 0xea, 0xe9, 0xe8, 0xe7, 0xe6, 0xe5, 0xe4, 0xe3, 0xe2, 0xe1, 0xe0, 0xdf, 0xde, 0xdd, 0xdc, 0xdb, 0xda, 0xd9, 0xd8, 0xd7, 0xd6, 0xd5, 0xd4, 0xd3, 0xd2, 0xd1, 0xd0, 0xcf, 0xce, 0xcd, 0xcc, 0xcb, 0xca, 0xc9, 0xc8, 0xc7, 0xc6, 0xc5, 0xc4, 0xc3, 0xc2, 0xc1, 0xc0, 0xbf, 0xbe, 0xbd, 0xbc, 0xbb, 0xba, 0xb9, 0xb8, 0xb7, 0xb6, 0xb5, 0xb4, 0xb3, 0xb2, 0xb1, 0xb0, 0xaf, 0xae, 0xad, 0xac, 0xab, 0xaa, 0xa9, 0xa8, 0xa7, 0xa6, 0xa5, 0xa4, 0xa3, 0xa2, 0xa1, 0xa0, 0x9f, 0x9e, 0x9d, 0x9c, 0x9b, 0x9a, 0x99, 0x98, 0x97, 0x96, 0x95, 0x94, 0x93, 0x92, 0x91, 0x90, 0x8f, 0x8e, 0x8d, 0x8c, 0x8b, 0x8a, 0x89, 0x88, 0x87, 0x86, 0x85, 0x84, 0x83, 0x82, 0x81, 0x80, 0x7f, 0x7e, 0x7d, 0x7c, 0x7b, 0x7a, 0x79, 0x78, 0x77, 0x76, 0x75, 0x74, 0x73, 0x72, 0x71, 0x70, 0x6f, 0x6e, 0x6d, 0x6c, 0x6b, 0x6a, 0x69, 0x68, 0x67, 0x66, 0x65, 0x64, 0x63, 0x62, 0x61, 0x60, 0x5f, 0x5e, 0x5d, 0x5c, 0x5b, 0x5a, 0x59, 0x58, 0x57, 0x56, 0x55, 0x54, 0x53, 0x52, 0x51, 0x50, 0x4f, 0x4e, 0x4d, 0x4c, 0x4b, 0x4a, 0x49, 0x48, 0x47, 0x46, 0x45, 0x44, 0x43, 0x42, 0x41, 0x40, 0x3f, 0x3e, 0x3d, 0x3c, 0x3b, 0x3a, 0x39, 0x38, 0x37, 0x36, 0x35, 0x34, 0x33, 0x32, 0x31, 0x30, 0x2f, 0x2e, 0x2d, 0x2c, 0x2b, 0x2a, 0x29, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21, 0x20, 0x1f, 0x1e, 0x1d, 0x1c, 0x1b, 0x1a, 0x19, 0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x10, 0x0f, 0x0e, 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 }; unsigned char i = 0; serial_port_one_initial( ); //initialize the major uart seial_port_two_initial( ); //initialize the secondary uart(s2) display_mcu_start_led( ); //open mcu-start-led, mcu start to work send_uart_two(0x55); //send data0x55 by the secondary uart send_uart_two(0xaa); //send data0xaa by the secondary uart for (array_point = 0; array_point<512; array_point++) { send_uart_two (test array_one[array_point]); } send_uart_one(0x34); //send data0x34 by the major uart send_uart_one(0xa7); //send data0xa7 by the major uart stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 260 www.stcmcu.com for (array_point = 0; array_point<512; array_point++) { send_uart_one (test array_one[array_point]); } send_pwm( ); //6khz pwm, 50% duty while(1) } void serial_port_one_initial( ) { scon = 0x50; //0101,0000 8-bit variable baud rate, no parity bit // tmod = 0x21; //0011, 0001 set timer 1 for 8-bit auto-reload mode // th1 = reload_count; //load timer 1 auto-reload value // tl1 = reload_count; // tr1 = 1; // enable timer 1 brt = reload_count; // brtr = 1, s1brs = 1, extram = 1, enable extram auxr = 0x11; // t0x12, t1x12, uart_m0x6, brtr, s2smod, brtx12, extram, s1brs es = 1; // enable serial port interrupt ea = 1; // set the global enable bit } void serial_port_two_initial( ) { //sfr scon = 0x98; //sm0, sm1, sm2, ren, tb8, rb8, ti, ri //sfr s2con = 0x9a; //s2sm0, s2sm1, s2sm2, s2ren, s2tb8, s2rb8, s2ti, s2ri //sfr s2buf = 0x9b; //sfr ie2 = 0xaf; //x, x, x, x, x, x, espi, es2 s2con = 0x50; //0101, 0000 8-bit variable baud rate, no parity bit, brt = reload_count; // brtr =1, s1brs = 1, extram = 0, enable extram auxr = 0x11; //t0x12, t1x12, uart_m0x6, brtr, s2smod, brtx12, extram, s1brs // es = 1; // enable the major uart interrupt // es2 = 1; ie2 = 0x01; // enable the secondary uart interrupt, es2=1 ea = 1; // set the global enable bit } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 261 www.stcmcu.com void send_uart_one(unsigned char i) { es = 0; //disable serial port interrupt ti = 0; //clear serial port transmit interrupt flag subf = i; while (ti ==0); //wait to finish transmitting ti = 0; // clear serial port transmit interrupt flag es = 1; //enable serial port interrupt } void send_uart_two(unsigned char i) { //sfr scon = 0x98; //sm0, sm1, sm2, ren, tb8, rb8, ti, ri //sfr s2con = 0x9a; //s2sm0, s2sm1, s2sm2, s2ren, s2tb8, s2rb8, s2ti, s2ri //sfr s2buf = 0x9b; //sfr ie2 = 0xaf; //x, x, x, x, x, x, espi, es2 unsigned char temp = 0; // es = 0; // disable the major uart interrupt ie2 = 0x00; // disable the secondary uart interrupt, es2=0 // ti = 0; //clear the major uart transmit interrupt flag s2con = s2con & 0xfd; //b' 11111101,clear the secondary uart transmit interrupt flag // sbuf = i; s2buf = i; // while(ti == 0); //wait to finish transmitting do { temp = s2con; temp = temp & 0x02; }while(temp == 0); // ti = 0; //clear the major uart transmit interrupt flag s2con = s2con & 0xfd; //b' 11111101,clear the secondary uart transmit interrupt flag // es = 1; // enable the major uart interrupt // es2 = 1; ie2 = 0x01; // enable the secondary uart interrupt, es2=1 } void uart_one_interrupt_receive(void) interrupt 4 { unsigned char k = 0; if (ri ==1) { ri = 0; k = sbuf; stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 262 www.stcmcu.com if (k==self_define_isp_download_command) //self-define download-command { delay( ); // to delay 1s is enough delay( ); // to delay 1s is enough soft_reset_to_isp_monitor( ); //soft-reset to isp-monitor } send_uart_one(k+1); } else { ti =0; } } void uart_two_interrupt_receive(void) interrupt 8 { //sfr scon = 0x98; //sm0, sm1, sm2, ren, tb8, rb8, ti, ri //sfr s2con = 0x9a; //s2sm0, s2sm1, s2sm2, s2ren, s2tb8, s2rb8, s2ti, s2ri //sfr s2buf = 0x9b; //sfr ie2 = 0xaf; //x, x, x, x, x, x, espi, es2 unsigned char k = 0; k = s2con; k = k & 0x01; //if (s2ri == 1) if ( k==1) { //ri = 0; s2con = s2con & 0xfe; //1111,1110 s2_interrupt_receive_led = 0; k = s2buf; if (k==self_define_isp_download_command) //self-define isp download-command { delay( ); //to delay 1s is enough delay( ); //to delay 1s is enough soft_reset_to_isp_monitor( ); //soft-reset to isp-monitor } send_uart_two(k+1); } else { //ti =0; s2con = s2con & 0xfd; //1111, 1101 } } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 263 www.stcmcu.com void soft_reset_to_isp_monitor (void) { iap_contr = 0x60; //0110,0000 soft-reset to isp-monitor } void delay (void) { unsigned int j = 0; unsigned int g = 0; for(j=0; j<5; j++) { for(g=0; g<60000; g++) { _nop_( ); _nop_( ); _nop_( ); _nop_( ); _nop_( ); } } } void display_mcu_start_led(void) { //sbit mcu_start_led = p1^7; for (i=0; i<1; i++) { mcu_start_led = 0; //turn on mcu-start-led delay( ); mcu_start_led = 1; //turn off mcu-start-led delay( ); mcu_start_led = 0; //turn on mcu-start-led } } void send_pwm (void) { cmod = 0x00; //cidl---------------cps1 cps2 ecf setup pca timer //cps1 cps2 = 00, sysclk/12 is pca/pwm clock //18432000/12/256 = 6000 cl = 0x00; ch = 0x00; ccap0l = 0x80; //set the initial value same as ccap0h ccap0h = 0x80; //50% duty cycle ccapm0= 0x42; //0100,0010 setup pca module 0 in 8-bit pwm, p3.7 cr = 1 //setup pca/pwm timer } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 264 www.stcmcu.com assembly program /*-------------------------------------------------------------------------------*/ /* --- stc mcu international limited ----------------------------------*/ /* --- stc12c5axx series mcu uart2 communicaton demo ----*/ /* --- mobile: (86)13922809991 ------------------------------------------*/ /* --- fax: 86-755-82905966 ----------------------------------------------*/ /* --- tel: 86-755-82948412 -----------------------------------------------*/ /* --- web: www.stcmcu.com -----------------------------------------*/ /* if you want to use the program or the program referenced in the */ /* article, please specify in which data and procedures from stc */ /*-------------------------------------------------------------------------------*/ s2con equ 9ah ;s2sm0, s2sm1, s2sm2, s2ren, s2tb8, s2rb8, s2ti, s2ri ie2 equ 0afh ;x, x, x, x, x, x, espi, es2 s2buf equ 9bh auxr equ 8eh brt equ 9ch iap_contr equ 0c7h relaod_count equ 0fbh ;18.432mhz, 12t, smod = 0, 9600bps ;relaod_count equ 0f6h ;18.432mhz, 12t, smod = 0, 4800bps ;relaod_count equ 0ech ;18.432mhz, 12t, smod = 0, 2400bps ;relaod_count equ 0d8h ;18.432mhz, 12t, smod = 0 , 1200bps ;--------------------------------------------------------------------------------------------------------------------- org 0000h ljmp main org 0043h ljmp uart_two_interrupt_receive org 0100h main: mov sp, #0c0h lcall uart2_initial mov 11h, #55h lcall send_uart_two mov 11h, #0aah lcall send_uart_two sjmp $ stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 265 www.stcmcu.com uart2_initial: push acc mov scon, #50h ;0101,0000 8-bit variable baud rate, no parity mov brt, #reload_count; mov auxr, #11h ;t0x12,t1x12,uart_m0x6,brtr,s2smod,brtx12,extram,s1brs ;brtr=1, s1brs=1, extram=0 enable extram mov ie2, #01h ;enable the secondary uart interrupt, es2=1 settb ea ;set the global enable bit pop acc ret ;--------------------------------------------------------------------------------------------------------------------------------------- send_uart_two: push acc mov ie2, #00h ;disable the secondary uart interrupt, es2=0 mov a, s2con ;1111,1101, clear secondary uart transimit interrupt flag anl a, #0fdh mov s2con, a mov s2buf, 11h uart2_send_wait: mov a, s2con anl a, #02h ;0000,0010 cjne a, #02h, uart_send_wait mov a, s2con anl a, #0fdh ;1111,1101, clear secondary uart transimit interrupt flag mov s2con, a mov ie2, #01h ;enable the secondary uart interrupt, es2=1 pop acc ret ;--------------------------------------------------------------------------------------------------------------------------------------- uart_two_interrupt_receive: puss acc mov a, s2con anl a, #01h cjne a, #01h, clear_s2ti_reti mov a, s2con anl a, #0feh ;1111,1110 mov s2con, a mov 11h, s2buf inc 11h lcall send_uart_two pop acc reti stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 266 www.stcmcu.com clear_s2ti_reti: mov a, s2con anl a, #0fdh ;1111,1101 mov s2con, a pop acc reti end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 267 www.stcmcu.com chapter 9. analog to digital converter adc_power speed1 speed0 adc_flag adc_start chs2 chs1 chs0 adc7/p1.7 adc6/p1.6 adc5/p1.5 adc4/p1.4 adc3/p1.3 adc2/p1.2 adc1/p1.1 adc0/p1.0 + - successive approximation register 10-bit dac adc result register : adc_ res and adc_resl comparator adc_contr register 9.1 a/d converter structure analog input signal channel select switch chs2/chs1/chs0 adc_b9 adc_b8 adc_b7 adc_b6 adc_b5 adc_b4 adc_b3 adc_b2 adc_res[7:0] adc_resl[1:0] - - - - - - adc_b1 adc_b0 if auxr.1/adrj = 0, adc result register format is shown as below: - - - - - - adc_b9 adc_b8 adc_res[1:0] adc_resl[7:0] adc_b7 adc_b6 adc_b5 adc_b4 adc_b3 adc_b2 adc_b1 adc_b0 if auxr.1/adrj = 1, adc result register format is shown as below: stc12c5a60s2 series mcu with a/d conversion function have 8-channel and 10-bit high-speed a/d converters whose speed is up to 250khz (250 thousand times per second). the 8-channel adc, which are on p1 port (p1.0-p1.7) , can be used as temperature detection, battery voltage detection, key scan, spectrum detection, etc. after power on reset, p1 ports are in weak pull-up mode. users can set any one of 8 channels as a/d conversion through software. and those i/o ports not as adc function can continue to be used as i/o ports. stc12c5a60s2 series mcu adc (a/d converter) structure is shown below. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 268 www.stcmcu.com conversion is invoked since adc_strat(adc_contr.3) bit is set. before invoking conversion, adc_power/adc_contr.7 bit should be set first in order to turn on the power of analog front-end in adc circuitry. prior to adc conversion, the desired i/o ports for analog inputs should be configured as input- only or open-drain mode first. the converter takes around a fourth cycles to sample analog input data and other three fourths cycles in successive-approximation steps. total conversion time is controlled by two register bits C speed1 and speed0. eight analog channels are available on p1 and only one of them is connected to to the comparator depending on the selection bits {chs2,chs1,chs0}. when conversion is completed, the result will be saved onto {adc_res,adc_resl[1:0]} register if auxr1.2(adrj) =0 or saved onto {adc_res[1:0],adc_resl} if adrj=1 . after the result are completed and saved, adc_flag is also set. adc_flag associated with its enable register ie.5(eadc). adc_flag should be cleared in software. the adc interrupt service routine vectors to 2bh . when the chip enters idle mode or power-down mode, the power of adc is gated off by hardware. when adrj = 0, if user need 10-bit conversion result, calculating the result according to the following formula: when adrj = 0, if user need 8-bit conversion result, calculating the result according to the following formula: when adrj = 1, if user need 10-bit conversion result, calculating the result according to the following formula: 10-bit a/d conversion result:(adc_res[7:0], adc_resl[1:0]) = 1024 x vin vcc 8-bit a/d conversion result:(adc_res[7:0])= 256 x vin vcc 10-bit a/d conversion result:(adc_res[1:0], adc_resl[7:0]) = 1024 x vin vcc in the above formulas, vin stand for analog input channel voltage, vcc stand for actual operation voltage. the adc on stc12c5a60s2 is an 10-bit resolution, successive-approximation approach, medium-speed a/d converter. v refp/ v refm is the positive/negative reference voltage input for internal voltage-scaling dac use, the typical sink current on it is 600ua ~ 1ma. for stc12c5a60s2, these two references are internally tied to vcc and gnd separately. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 269 www.stcmcu.com 2. adc control register: adc_contr (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 adc_contr bch name adc_power speed1 speed0 adc_flag adc_start chs2 chs1 chs0 when operating to adc_contr register, "mov" should be used, while "and" and "or" don not be recommended to use adc_power : when clear shut down the power of adc block. when set turn on the power of adc block. speed1, speed0 : conversion speed selection. speed1 speed0 times needed by an a/d coversion 0 0 540 clock cycles are needed for a conversion. 0 1 360 clock cycles are needed for a conversion. 1 0 180 clock cycles are needed for a conversion. 1 1 90 clock cycles are needed for a conversion. when the cpu operation frequency is 12mhz, the speed of adc is about 250khz. the clock source used by adc block of stc12c5a60s2 series mcu is on-chip r/c clock which is not divided by clock divider register clk_div. adc_flag : adc interrupt flag.it will be set by the device after the device has finished a conversion, and should be cleared by the user's software. 9.2 registers for adc 1. p1 analog function configure register: p1asf (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 p1asf 9dh name p17asf p16asf p15asf p14asf p13asf p12asf p11asf p10asf p1xasf 0 : = keep p1.x as general-purpose i/o function. 1 : = set p1.x as adc input channel-x mnemonic description address bit address and symbol msb lsb reset value p1asf p1 analog function configure register 9dh p17asf p16asf p15asf p14asf p13asf p12asf p11asf p10asf 0000 0000b adc_contr adc control register bch adc_power speed1 speed0 adc_flag adc_start chs2 chs1 chs0 0000 0000b adc_res adc result high bdh 0000 0000b adc_resl adc result low beh 0000 0000b auxr1 auxiliary register 1 a2h - pca_p4 spi_p4 s2_p4 gf2 adrj - dps x000 00x0b ie interrupt enable a8h ea elvd eadc es et1 ex1 et0 ex0 0000 0000b ip interrupt priority low b8h ppca plvd padc ps pt1 px1 pt0 px0 0000 0000b iph interrupt priority high b7h ppcah plvdh padch psh pt1h px1h pt0h px0h 0000 0000b stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 270 www.stcmcu.com adc_strat : adc start bit, which enable adc conversion.it will automatically cleared by the device after the device has finished the conversion. chs2 ~ chs0 : used to select one analog input source from 8 channels. chs2 chs1 chs0 source 0 0 0 p1.0 (default) as the a/d channel input 0 0 1 p1.1 as the a/d channel input 0 1 0 p1.2 as the a/d channel input 0 1 1 p1.3 as the a/d channel input 1 0 0 p1.4 as the a/d channel input 1 0 1 p1.5 as the a/d channel input 1 1 0 p1.6 as the a/d channel input 1 1 1 p1.7 as the a/d channel input note : the corresponding bits in p1asf should be configured correctly before starting a/d conversion. the sepecific p1asf bits should be set corresponding with the desired channels. because it will by delayed 4 cpu clocks after the instruction which set adc_contr register has been executed, four "nop" instructions should be added after setting adc_contr register. see the following code: mov adc_contr, #data nop nop nop nop mov a, adc_contr ;only delayed 4 clocks, can the adc_contr be read correctly. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 271 www.stcmcu.com 3. adc result register: adc_res and adc_resl adc_res and adc_resl are used to save the adc result, their format as shown below: mnemonic add name b7 b6 b5 b4 b3 b2 b1 b0 adc_res bdh adc result register high adc_resl beh adc result register low auxr1 a2h auxiliary register1 - pca_p4 spi_p4 s2_p4 gf2 adrj - dps the adc_res and adc_resl are the final result from the adc. adrj/auxr.1 is the control bit of adc result arrangement in adc result registers (adc_res, adc_resl). if adrj=0, the higher 8 bits of 10 bits adc result are arranged in adc_res, and the lower 2 bits are in adc_resl. see the following table. mnemonic add name b7 b6 b5 b4 b3 b2 b1 b0 adc_res bdh adc result register high adc_res9 adc_res8 adc_res7 adc_res6 adc_res5 adc_res4 adc_res3 adc_res2 adc_resl beh adc result register low - - - - - - adc_res0 adc_res1 auxr1 a2h auxiliary register1 adrj=0 if user need the full 10-bit conversion result, calculating the result according to the following formula: if user only need 8-bit conversion result, calculating the result according to the following formula: 10-bit a/d conversion result:(adc_res[7:0], adc_resl[1:0]) = 1024 x vin vcc 8-bit a/d conversion result:(adc_res[7:0])= 256 x vin vcc in the above formulas, vin stand for analog input channel voltage, vcc stand for actual operation voltage. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 272 www.stcmcu.com if adrj=1, the higher 2 bits of 10 bits adc result are arranged in adc_res, and the lower 8 bits are in adc_resl. see the following table. mnemonic add name b7 b6 b5 b4 b3 b2 b1 b0 adc_res bdh adc result register high adc_res9 adc_res8 adc_resl beh adc result register low adc_res7 adc_res6 adc_res5 adc_res4 adc_res3 adc_res2 adc_res0 adc_res1 auxr1 a2h auxiliary register1 adrj=1 calculating the full 10-bit conversion result according to the following formula: 10-bit a/d conversion result:(adc_res[1:0], adc_resl[7:0]) = 1024 x vin vcc in the above formulas, vin stand for analog input channel voltage, vcc stand for actual operation voltage. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 273 www.stcmcu.com 4. auxr1: adc result arrangement register mnemonic address bit 7 6 5 4 3 2 1 0 auxr1 a2h name - pca_p4 spi_p4 s2_p4 gf2 adrj - dps pca _p4 0 : default. the pca function is on p1[4:2] 1 : the pca function on p1[4:2] is switched to p4[3:1]. eci is switched from p1.2 to p4.1 pca0/pwm0 is switched from p1.3 to p4.2 pca1/pwm1 is switched from p1.4 to p4.3 spi_p4 0 : default. the spi function is on p1[7:4] 1 : the spi function on p1[7:4] is switched to p4[3:0]. sclk is switched from p1.7 to p4.3 mosi is switched from p1.6 to p4.2 miso is switched from p1.5 to p4.1 ss is switched from p1.4 to p4.0 s2_p4 0 : default. the uart2(s2) function is on p1[3:2] 1 : the uart2(s2) function on p1[3:2] is switched to p4[3:2]. txd2 is switched from p1.3 to p4.3 rxd2 is switched from p1.2 to p4.2 gf2 : general flag. it can be used by software. adrj 0 : the 10-bit conversion result of adc is arranged as {adc_res[7:0], adc_resl[1:0]}. 1 : the 10-bit conversion result is right-justified, {adc_res[1:0], adc_resl[7:0]}. dps 0 : default. dptr0 is selected as data pointer. 1 : the secondary dptr is switched to use. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 274 www.stcmcu.com 5. registers related with uart1 interrupt : ie, ip and iph ie: interrupt enable rsgister (bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ie a8h name ea elvd eadc es et1 ex1 et0 ex0 ea : disables all interrupts. if ea = 0,no interrupt will be acknowledged. if ea = 1, each interrupt source is individually enabled or disabled by setting or clearing its enable bit. eadc: adc interrupt enable bit. if eadc = 0, adc interrupt will be diabled. if eadc = 1, adc interrupt is enabled. iph: interrupt priority high register (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 iph b7h name ppcah plvdh padch psh pt1h px1h pt0h px0h ip: interrupt priority register (bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ip b8h name ppca plvd padc ps pt1 px1 pt0 px0 padch, padc: adc interrupt priority control bits. if padch=0 and padc=0, adc interrupt is assigned lowest priority (priority 0). if padch=0 and padc=1, adc interrupt is assigned lower priority (priority 1). if padch=1 and padc=0, adc interrupt is assigned higher priority (priority 2). if padch=1 and padc=1, adc interrupt is assigned highest priority (priority 3). stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 275 www.stcmcu.com 9.3 application circuit of a/d converter 2 8 27 26 25 24 23 22 21 20 19 18 17 16 15 vcc p2.1 p2.7 p1.7/adc7 p1.5/adc5 p1.4/adc4 p1.3/adc3 gnd p1.1/adc1 p1.0/adc0 p1.2/adc2 p2.2 rst p2.5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 p1.6/adc6 p2.4 p2.0 p3.7/pca0/pwm0 sop-28/skdip-28 adc function in p1 port, p1.0 - p1.7 in all 8 channels 1k signal source rxd/p3.0 txd/p3.1 xtal2 xtal1 int0/p3.2 int1/p3.3 clkout1/pwm1/pca1/t1/p3.5 clkout0/eci/t0/p3.4 p2.3 p2.6 more than 47pf stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 276 www.stcmcu.com 9.4 adc application circuit for key scan vcc 10k 10k 10k 10k 10k 0v 1/2 vcc 2/3 vcc 3/4 vcc 4/5 vcc 2 8 27 26 25 24 23 22 21 20 19 18 17 16 15 vcc p2.1 p2.7 p1.7/adc7 p1.5/adc5 p1.4/adc4 p1.3/adc3 gnd p1.1/adc1 p1.0/adc0 p1.2/adc2 p2.2 rst p2.5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 p1.6/adc6 p2.4 p2.0 p3.7/pca0/pwm0 sop-28/skdip-28 1k rxd/p3.0 txd/p3.1 xtal2 xtal1 int0/p3.2 int1/p3.3 clkout1/pwm1/pca1/t1/p3.5 clkout0/eci/t0/p3.4 p2.3 p2.6 more than 47pf adc function in p1 port, p1.0 - p1.7 in all 8 channels this curcuit can realize the single key scan and assembling key scan detection function, but the value of resistors should be adjusted according to the actual demand. r6 8.2k sw6 r5 5.4k sw5 r4 3.3k sw4 r3 1.8k sw3 r2 520 sw2 sw1 r1 10k +5v adcx 47pf 0 0`0.5 0.5`1 1`1.5 1.5`2.0 2.0`2.5 2 8 27 26 25 24 23 22 21 20 19 18 17 16 15 vcc p2.1 p2.7 p1.7/adc7 p1.5/adc5 p1.4/adc4 p1.3/adc3 gnd p1.1/adc1 p1.0/adc0 p1.2/adc2 p2.2 rst p2.5 1 2 3 4 5 6 7 8 9 10 11 12 13 14 p1.6/adc6 p2.4 p2.0 p3.7/pca0/pwm0 sop-28/skdip-28 rxd/p3.0 txd/p3.1 xtal2 xtal1 int0/p3.2 int1/p3.3 clkout1/pwm1/pca1/t1/p3.5 clkout0/eci/t0/p3.4 p2.3 p2.6 10 keys are used to divide the voltage in the below curcuit, what the error of each key float between -0.25v and +0.25v can effectively avoid that resistance error or temprature difit lead to disable key detection. r1 520 sw2 sw1 r0 10k +5v 47pf 0 r2 1.2k sw3 r3 1.6k sw4 r4 1.8k sw5 r5 3k sw6 r6 4k sw7 r7 6.5 sw8 r8 10k sw9 r9 30k sw10 r10 100k sw11 adcx stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 277 www.stcmcu.com 9.5 a/d reference voltage source stc12c5axx series adc reference voltage is from mcu power supply voltage directly, so it can work without an external reference voltage source. if the required precision is relatively high, then you maybe using a stable reference voltage source, in order to calculate the operating voltage vcc, then calculate the adc exact value. for example, you can connect a 1.25v(or 1.00v, ect. ) to adc channel 7, according to the conversion result, you can get the actual vcc voltage, thus you can calculate other 7 channels adc results. (vcc is constant in short time) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 278 www.stcmcu.com 9.6 program using interrupts to demostrate a/d conversion there are two example procedures using interrupts to demostrate a/d conversion, one written in c langugage and the other in assembly language. 1. c language code listing: /*---------------------------------------------------------------------------------*/ /* --- stc mcu international limited ------------------------------------*/ /* --- stc 1t series mcu a/d conversion demo ----------------------*/ /* --- mobile: (86)13922809991 --------------------------------------------*/ /* --- fax: 86-755-82905966 ------------------------------------------------*/ /* --- tel: 86-755-82948412 -------------------------------------------------*/ /* --- web: www.stcmcu.com --------------------------------------------*/ /* if you want to use the program or the program referenced in the */ /* article, please specify in which data and procedures from stc */ /*---------------------------------------------------------------------------------*/ #include "reg51.h" #include "intrins.h" #define fosc 18432000l #define baud 9600 typedef unsigned char byte; typedef unsigned int word; /*declare sfr associated with the adc */ sfr adc_contr = 0xbc; //adc control register sfr adc_res = 0xbd; //adc hight 8-bit result register sfr adc_low2 = 0xbe; //adc low 2-bit result register sfr p1asf = 0x9d; //p1 secondary function control register /*define adc operation const for adc_contr*/ #define adc_power 0x80 //adc power control bit #define adc_flag 0x10 //adc complete flag #define adc_start 0x08 //adc start control bit #define adc_speedll 0x00 //540 clocks #define adc_speedl 0x20 //360 clocks #define adc_speedh 0x40 //180 clocks #define adc_speedhh 0x60 //90 clocks void inituart(); void senddata(byte dat); void delay(word n); void initadc(); byte ch = 0; //adc channel no. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 279 www.stcmcu.com void main() { inituart(); //init uart, use to show adc result initadc(); //init adc sfr ie = 0xa0; //enable adc interrupt and open master interrupt switch //start a/d conversion while (1); } /*----------------------------------------- adc interrupt service routine --------------------------------------------*/ void adc_isr( ) interrupt 5 using 1 { adc_contr &= !adc_flag; //clear adc interrupt flag senddata(ch); //show channel no. senddata(adc_res); //get adc high 8-bit result and send to uart //if you want show 10-bit result, uncomment next line // senddata(adc_low2); //show adc low 2-bit result if (++ch > 7) ch = 0; //switch to next channel adc_contr = adc_power | adc_speedll | adc_start | ch; } /*-------------------------------- initial adc sfr ------------------------------------*/ void initadc( ) { p1asf = 0xff; //set all p1 as analog input port adc_res = 0; //clear previous result adc_contr = adc_power | adc_speedll | adc_start | ch; delay(2); //adc power-on delay and start a/d conversion } /*----------------------------------- initial uart --------------------------------------*/ void inituart() { scon = 0x5a; //8 bit data ,no parity bit tmod = 0x20; //t1 as 8-bit auto reload th1 = tl1 = -(fosc/12/32/baud); //set uart baudrate tr1 = 1; //t1 start running } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 280 www.stcmcu.com /*------------------------------------- send one byte data to pc input: dat (uart data) output:- -----------------------------------------*/ void senddata(byte dat) { while (!ti); //wait for the previous data is sent ti = 0; //clear ti flag sbuf = dat; //send current data } /*---------------------------------------- software delay function -------------------------------------------*/ void delay(word n) { word x; while (n--) { x = 5000; while (x--); } } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 281 www.stcmcu.com 2. assembly language code listing: ;/*----------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited -------------------------------------*/ ;/* --- stc 1t series mcu a/d conversion demo -----------------------*/ ;/* --- mobile: (86)13922809991 ---------------------------------------------*/ ;/* --- fax: 86-755-82905966 -------------------------------------------------*/ ;/* --- tel: 86-755-82948412 --------------------------------------------------*/ ;/* --- web: www.stcmcu.com ---------------------------------------------*/ ;/* if you want to use the program or the program referenced in the */ ;/* article, please specify in which data and procedures from stc */ ;/*-----------------------------------------------------------------------------------*/ ;/*declare sfr associated with the adc */ adc_contr equ 0bch ;adc control register adc_res equ 0bdh ;adc high 8-bit result register adc_low2 equ 0beh ;adc low 2-bit result register p1asf equ 09dh ;p1 secondary function control register ;/*define adc operation const for adc_contr*/ adc_power equ 80h ;adc power control bit adc_flag equ 10h ;adc complete flag adc_start equ 08h ;adc start control bit adc_speedll equ 00h ;540 clocks adc_speedl equ 20h ;360 clocks adc_speedh equ 40h ;180 clocks adc_speedhh equ 60h ;90 clocks adcch data 20h ;adc channel no. ;------------------------------------------------------------------------------------------ org 0000h ljmp main org 002bh ljmp adc_isr ;------------------------------------------------------------------------------------------- org 0100h main: mov sp, #3fh mov adcch, #0 lcall init_uart ;init uart, use to show adc result lcall init_adc ;init adc sfr mov ie, #0a0h ;enable adc interrupt ;and open master interrupt switch sjmp $ stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 282 www.stcmcu.com ;/*----------------------------------------- ;adc interrupt service routine ;-------------------------------------------*/ adc_isr: push acc push psw anl adc_contr, #not adc_flag ;clear adc interrupt flag mov a, adcch lcall send_data ;send channel no. mov a, adc_res ;get adc high 8-bit result lcall send_data ;send to uart ;//if you want show 10-bit result, uncomment next 2 lines ; mov a, adc_low2 ;get adc low 2-bit result ; lcall send_data ;send to uart inc adcch mov a, adcch anl a, #07h mov adcch, a orl a, #adc_power | adc_speedll | adc_start mov adc_contr, a ;adc power-on delay ;and re-start a/d conversion pop psw pop acc reti ;/*----------------------------------- ;initial adc sfr ;--------------------------------------*/ init_adc: mov p1asf, #0ffh ;set all p1 as analog input port mov adc_res, #0 ;clear previous result mov a, adcch orl a, #adc_power | adc_speedll | adc_start mov adc_contr, a ;adc power-on delay ;and start a/d conversion mov a, #2 lcall delay ret ;/*------------------------------------- ;initial uart ;----------------------------------------*/ init_uart: mov scon, #5ah ;8 bit data ,no parity bit mov tmod, #20h ;t1 as 8-bit auto reload mov a, #-5 ;set uart baudrate -(18432000/12/32/9600) mov th1, a ;set t1 reload value mov tl1, a setb tr1 ;t1 start running ret stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 283 www.stcmcu.com ;/*---------------------------------------- ;send one byte data to pc ;input: acc (uart data) ;output:- ;--------------------------------------------*/ send_data: jnb ti, $ ;wait for the previous data is sent clr ti ;clear ti flag mov sbuf, a ;send current data ret ;/*-------------------------------------------- ;software delay function ;-----------------------------------------------*/ delay: mov r2, a clr a mov r0, a mov r1, a delay1: djnz r0, delay1 djnz r1, delay1 djnz r2, delay1 ret end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 284 www.stcmcu.com 9.7 program using polling to demostrate a/d conversion there are two example procedures using inquiry to demostrate a/d conversion, one written in c langugage and the other in assembly language. 1. c language code listing: /*--------------------------------------------------------------------------------*/ /* --- stc mcu international limited -----------------------------------*/ /* --- stc 1t series mcu a/d conversion demo ---------------------*/ /* --- mobile: (86)13922809991 ------------------------------------------*/ /* --- fax: 86-755-82905966 ----------------------------------------------*/ /* --- tel: 86-755-82948412 -----------------------------------------------*/ /* --- web: www.stcmcu.com -----------------------------------------*/ /* if you want to use the program or the program referenced in the */ /* article, please specify in which data and procedures from stc */ /*------------------------------------------------------------------------------*/ #include "reg51.h" #include "intrins.h" #define fosc 18432000l #define baud 9600 typedef unsigned char byte; typedef unsigned int word; /*declare sfr associated with the adc */ sfr adc_contr = 0xbc; //adc control register sfr adc_res = 0xbd; //adc high 8-bit result register sfr adc_low2 = 0xbe; //adc low 2-bit result register sfr p1asf = 0x9d; //p1 secondary function control register /*define adc operation const for adc_contr*/ #define adc_power 0x80 //adc power control bit #define adc_flag 0x10 //adc complete flag #define adc_start 0x08 //adc start control bit #define adc_speedll 0x00 //540 clocks #define adc_speedl 0x20 //360 clocks #define adc_speedh 0x40 //180 clocks #define adc_speedhh 0x60 //90 clocks void inituart(); void initadc(); void senddata(byte dat); byte getadcresult(byte ch); void delay(word n); void showresult(byte ch); stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 285 www.stcmcu.com void main() { inituart(); //init uart, use to show adc result initadc(); //init adc sfr while (1) { showresult(0); //show channel0 showresult(1); //show channel1 showresult(2); //show channel2 showresult(3); //show channel3 showresult(4); //show channel4 showresult(5); //show channel5 showresult(6); //show channel6 showresult(7); //show channel7 } } /*---------------------------------------------------------- send adc result to uart -------------------------------------------------------------*/ void showresult(byte ch) { senddata(ch); //show channel no. senddata(getadcresult(ch)); //show adc high 8-bit result //if you want show 10-bit result, uncomment next line // senddata(adc_low2); //show adc low 2-bit result } /*----------------------------------------------------------- get adc result --------------------------------------------------------------*/ byte getadcresult(byte ch) { adc_contr = adc_power | adc_speedll | ch | adc_start; _nop_(); //must wait before inquiry _nop_(); _nop_(); _nop_(); while (!(adc_contr & adc_flag)); //wait complete flag adc_contr &= ~adc_flag; //close adc return adc_res; //return adc result } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 286 www.stcmcu.com /*------------------------------------------------------------- initial uart ----------------------------------------------------------------*/ void inituart() { scon = 0x5a; //8 bit data ,no parity bit tmod = 0x20; //t1 as 8-bit auto reload th1 = tl1 = -(fosc/12/32/baud); //set uart baudrate tr1 = 1; //t1 start running } /*--------------------------------------------------------------- initial adc sfr -----------------------------------------------------------------*/ void initadc() { p1asf = 0xff; //open 8 channels adc function adc_res = 0; //clear previous result adc_contr = adc_power | adc_speedll; delay(2); //adc power-on and delay } /*------------------------------------------------------ send one byte data to pc input: dat (uart data) output:- ---------------------------------------------------------*/ void senddata(byte dat) { while (!ti); //wait for the previous data is sent ti = 0; //clear ti flag sbuf = dat; //send current data } /*-------------------------------------------------------- software delay function ----------------------------------------------------------*/ void delay(word n) { word x; while (n--) { x = 5000; while (x--); } } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 287 www.stcmcu.com 2. assembly language code listing: ;/*-------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited ----------------------------------*/ ;/* --- stc 1t series mcu a/d conversion demo --------------------*/ ;/* --- mobile: (86)13922809991 ------------------------------------------*/ ;/* --- fax: 86-755-82905966 ----------------------------------------------*/ ;/* --- tel: 86-755-82948412 -----------------------------------------------*/ ;/* --- web: www.stcmcu.com -----------------------------------------*/ ;/* if you want to use the program or the program referenced in the */ ;/* article, please specify in which data and procedures from stc */ ;/*------------------------------------------------------------------------------*/ ;/*declare sfr associated with the adc */ adc_contr equ 0bch ;adc control register adc_res equ 0bdh ;adc high 8-bit result register adc_low2 equ 0beh ;adc low 2-bit result register p1asf equ 09dh ;p1 secondary function control register ;/*define adc operation const for adc_contr*/ adc_power equ 80h ;adc power control bit adc_flag equ 10h ;adc complete flag adc_start equ 08h ;adc start control bit adc_speedll equ 00h ;540 clocks adc_speedl equ 20h ;360 clocks adc_speedh equ 40h ;180 clocks adc_speedhh equ 60h ;90 clocks ;------------------------------------------------------------------------------------------ org 0000h ljmp main ;------------------------------------------------------------------------------------------- org 0100h main: lcall init_uart ;init uart, use to show adc result lcall init_adc ;init adc sfr ;--------------------------------------------------------------------------------------------- next: mov a, #0 lcall show_result ;show channel0 result mov a, #1 lcall show_result ;show channel1 result mov a, #2 lcall show_result ;show channel2 result stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 288 www.stcmcu.com mov a, #3 lcall show_result ;show channel3 result mov a, #4 lcall show_result ;show channel4 result mov a, #5 lcall show_result ;show channel5 result mov a, #6 lcall show_result ;show channel6 result mov a, #7 lcall show_result ;show channel7 result sjmp next ;/*------------------------------------------------------------------- ;send adc result to uart ;input: acc (adc channel no.) ;output:- ;-----------------------------------------------------------------------*/ show_result: lcall send_data ;show channel no. lcall get_adc_result ;get high 8-bit adc result lcall send_data ;show result ;//if you want show 10-bit result, uncomment next 2 lines ; mov a, adc_low2 ;get low 2-bit adc result ; lcall send_data ;show result ret ;/*--------------------------------------------------------------------- ;read adc conversion result ;input: acc (adc channel no.) ;output:acc (adc result) ;------------------------------------------------------------------------*/ get_adc_result: orl a, #adc_power | adc_speedll | adc_start mov adc_contr,a ;start a/d conversion nop ;must wait before inquiry nop nop nop wait: mov a,adc_contr ;wait complete flag jnb acc.4, wait ;adc_flag(adc_contr.4) anl adc_contr, #not adc_flag ;clear adc_flag mov a, adc_res ;return adc result ret stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 289 www.stcmcu.com ;/*----------------------------------------------------------------------- ;initial adc sfr ;-------------------------------------------------------------------------*/ init_adc: mov p1asf, #0ffh ;open 8 channels adc function mov adc_res, #0 ;clear previous result mov adc_contr, #adc_power | adc_speedll mov a, #2 ;adc power-on and delay lcall delay ret ;/*------------------------------------------------------- ;initial uart ;----------------------------------------------------------*/ init_uart: mov scon, #5ah ;8 bit data ,no parity bit mov tmod, #20h ;t1 as 8-bit auto reload mov a, #-5 ;set uart baudrate -(18432000/12/32/9600) mov th1, a ;set t1 reload value mov tl1, a setb tr1 ;t1 start running ret ;/*-------------------------------------------------------- ;send one byte data to pc ;input: acc (uart data) ;output:- ;----------------------------------------------------------*/ send_data: nb ti, $ ;wait for the previous data is sent clr ti ;clear ti flag mov sbuf, a ;send current data ret ;/*---------------------------- ;software delay function ;----------------------------*/ delay: mov r2, a clr a mov r0, a mov r1, a delay1: djnz r0, delay1 djnz r1, delay1 djnz r2, delay1 ret end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 290 www.stcmcu.com chapter 10. programmable counter array(pca) the programmable counter array is a special 16-bit timer that has two 16-bit capture/compare modules associated with it. each of the modules can be programmed to operate in one of four modes: rising and/or falling edge capture(calculator of duty length for high/low pulse), software timer, high-speed output, or pulse width modulator. each module has a pin associated with it in port 1. module 0 is connected to pin p1.3, module 1 to pin p1.4 in stc12c5a60s2 series. while in stc12c5201ad series, module 0 is connected to pin p3.7, module 1 to pin p3.5. the pca timer is a common time base for all two modules and can be programmed to run at 1/12 the system clock, 1/2 the system clock, the timer 0 overflow or the input on eci pin(p1.2). the timer count source is determined from cps1 and cps0 bits in the cmod sfr. 10.2 sfrs related with pca pca/pwm sfrs table mnemonic description add bit address and symbol reset value b7 b6 b5 b4 b3 b2 b1 b0 ccon pca control register d8h cf cr - - - - ccf1 ccf0 00xx,xx00 cmod pca mode register d9h cidl - - - cps2 cps1 cps0 ecf 0xxx,0000 ccapm0 pca module 0 mode register dah - ecom0 capp0 capn0 mat0 tog0 pwm0 eccf0 x000,0000 ccapm1 pca module 1 mode register dbh - ecom1 capp1 capn1 mat1 tog1 pwm1 eccf1 x000,0000 cl pca base timer low e9h 0000,0000 ch pca base timer high f9h 0000,0000 ccap0l pca module-0 capture register low eah 0000,0000 ccap0h pca module-0 capture register high fah 0000,0000 ccap1l pca module-1 capture register low ebh 0000,0000 ccap1h pca module-1 capture register high fbh 0000,0000 pca_pwm0 pca pwm mode auxiliary register 0 f2h - - - - - - epc0h epc0l xxxx,xx00 pca_pwm1 pca pwm mode auxiliary register 1 f3h - - - - - - epc1h epc1l xxxx,xx00 auxr1 auxiliary register 1 a2h - pca_p4 spi_p4 s2_p4 gf2 adrj - dps x000,00x0 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 291 www.stcmcu.com 1. pca operation mode register: cmod (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 cmod d9h name cidl - - - cps2 cps1 cps0 ecf cidl : pca counter control bit in idle mode. if cidl=0, the pca counter will continue functioning during idle mode. if cidl=1, the pca counter will be gated off during idle mode. cps2, cps1, cps0 : pca counter pulse source select bits. cps2 cps1 cps0 select pca/pwm clock source 0 0 0 0, system clock/12, sysclk/12 0 0 1 1, system clock/2, sysclk/2 0 1 0 2, timer 0 overflow. pca/pwm clock can up to sysclk because timer 0 can operate in 1t mode. frequency-adjustable pwm output can be achieved by changing the timer 0 overflow. 0 1 1 3, exrenal clock from eci/p1.2 (or p4.1) pin (max speed = sysclk/2) 1 0 0 4, system clock, sysclk 1 0 1 5, system clock/4, sysclk/4 1 1 0 6, system clock/6, sysclk/6 1 1 1 7, system clock/8, sysclk/8 for example, if cps2/cps1/cps0=1/0/0, pca/pwm clock source is sysclk. if users need to select sysclk/3 as pca clock source, timer 0 should be set to operate in 1t mode and generate an overflow every 3 counting pulse. ecf : pca counter overflow interrupt enable bit. ecf=0 disables cf bit in ccon to generate an interrupt. ecf=1 enables cf bit in ccon to generate an interrupt. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 292 www.stcmcu.com 2. pca control register : ccon (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ccon d8h name cf cr - - - - ccf1 ccf0 cf : pca counter overflow flag. set by hardware when the counter rolls over. cf flags an interrupt if bit ecf in cmod is set. cf may be set by either hardware or software but can only be cleared by software. cr : pca counter run control bit. set by software to turn the pca counter on. must be cleared by software to turn the pca counter off. ccf1:pca module 1 interrupt flag. set by hardware when a match or capture from module 1 occurs.must be cleared by software. a match means the value of the pca counter equals the value of the capture/compare register in module 1. a capture means a specific edge from cex1 happens, so the capture/compare register latches the value of the pca counter, and the ccf1 is set. ccf0 :pca module 0 interrupt flag.set by hardware when a match or capture from module 0 occurs. must be cleared by software. 3. pca capture/campare register ccapm0 and ccapm1 each module in the pca has a special function register associated with it. these registers are ccapmn, n=0 ~1. ccapm0 for module 0 and ccapm1 for module 1. the register contains the bits that control the mode in which each module will operate. the eccfn bit enables the ccfn flag in the ccon sfr to generate an interrupt when a match or compare occurs in the associated module. pwmn enables the pulse width modulation mode. the togn bit when set causes the ccpn output associated with the module to toggle when there is a match between the pca counter and the modules capture/compare register. the match bit(matn) when set will cause the ccfn bit in the ccon register to be set when there is a match between the pca counter and the modules capture/ compare register. the next two bits capnn and cappn determine the edge that a capture input will be active on. the capnn bit enables the negative edge, and the cappn bit enables the positive edge. if both bits are set, both edges will be enabled and a capture will occur for either transition. the bit ecomn when set enables the comparator function. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 293 www.stcmcu.com capture/compare register of pca module 0 : ccapm0 (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ccapm0 dah name - ecom0 capp0 capn0 mat0 tog0 pwm0 eccf0 ecom0 : comparator enable bit. ecom0=0 disables the comparator function; ecom0=1 enables the comparator function. capp0 : capture positive control bit. capp0=1 enables positive edge capture. capn0 : capture negative control bit. capn0=1 enables negative edge capture. mat0 : match control bit. when mat0 = 1, a match of the pca counter with this modules compare/capture register causes the ccf0 bit in ccon to be set. tog0 : toggle control bit. when tog0=1, a match of the pca counter with this modules compare/capture register causes the ccp0 pin to toggle. (ccp0/pca0/pwm0/p1.3 or ccp0/pca0/pwm0/p4.2) or ccp0/pca0/pwm0/p4.2) ccp0/pca0/pwm0/p4.2) pwm0 : pulse width modulation. pwm0=1 enables the ccp0 pin to be used as a pulse width modulated output. (ccp0/pca0/pwm0/p1.3 or ccp0/pca0/pwm0/p4.2) or ccp0/pca0/pwm0/p4.2) ccp0/pca0/pwm0/p4.2) eccf0 : enable ccf0 interrupt. enables compare/capture flag ccf0 in the ccon register to generate an interrupt. capture/compare register of pca module 1 : ccapm1 (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ccapm1 dbh name - ecom1 capp1 capn1 mat1 tog1 pwm1 eccf1 ecom1 : comparator enable bit. ecom1=0 disables the comparator function; ecom1=1 enables the comparator function. capp1 : capture positive control bit. capp1=1 enables positive edge capture. capn1 : capture negative control bit. capn1=1 enables negative edge capture. mat1 : match control bit. when mat1 = 1, a match of the pca counter with this modules compare/capture register causes the ccf1 bit in ccon to be set. tog1 : toggle control bit. when tog1=1, a match of the pca counter with this modules compare/capture register causes the ccp1 pin to toggle. (ccp1/pca1/pwm1/p1.4 or ccp1/pca1/pwm1/p4.3) pwm1 : pulse width modulation. pwm1=1 enables the cex1 pin to be used as a pulse width modulated output. (ccp1/pca1/pwm1/p1.4 or ccp1/pca1/pwm1/p4.3) eccf1 : enable ccf1 interrupt. enables compare/capture flag ccf1 in the ccon register to generate an interrupt. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 294 www.stcmcu.com the operation mode of pca modules set as shown in the below table. setting the operation mode of pca modules ccapmn register n = 0,1 - ecomn cappn capnn matn togn pwmn eccfn function of pca modules 0 0 0 0 0 0 0 no operation 1 0 0 0 0 1 0 8-bit pwm, no interrupt 1 1 0 0 0 1 1 8-bit pwm output, interrupt can be generated on rising edge. 1 0 1 0 0 1 1 8-bit pwm output, interrupt can be generated on falling edge. 1 1 1 0 0 1 1 8-bit pwm output, interrupt can be generated on both rising and falling edges. x 1 0 0 0 0 x 16-bit capture mode, caputre triggered by the rising edge on ccpn/pcan pin x 0 1 0 0 0 x 16-bit capture mode, capture triggered by the falling edge on ccpn/pcan pin x 1 1 0 0 0 x 16-bit capture mode, capture triggered by the transition on ccpn/pcan pin 1 0 0 1 0 0 x 16-bit software timer 1 0 0 1 1 0 x 16-bit high-speed output 4. pca 16-bit counter low 8-bit cl and high 8-bit ch the addresses of cl and ch respectively are e9h and f9h, and their reset value both are 00h. cl and ch are used to save the pca load value. 5. pca capture/compare register ccapnl and ccapnh when pca is used to capture/compare, ccapnl and ccapnh are used to save the 16-bit capture value in corresponding block. when pca is operated in pwm mode, ccapnl and ccapnh are used to control the duty cycle of pwm output signal. "n=0 or 1" respectively stand for module 0 and 1. reset value of regsiters ccapnl and ccapnh are both 00h. their addresses respectively are: ccap0l eah, ccap0h fah : capture / compare register of module 0 ccap1l ebh, ccap1h fbh : capture / compare register of module 1 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 295 www.stcmcu.com 6. pwm registers of pca modules : pca_pwm0 and pca_pwm1 pca_pwm0 : pwm register of pca module 0 sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 pca_pwm0 f2h name - - - - - - epc0h epc0l b7 ~ b2 : reserved epc0h : associated with ccap0h, it is used in pca pwm mode. epc0l : associated with ccap0l, it is used in pca pwm mode. pca_pwm1 : pwm register of pca module 1 sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 pca_pwm1 f3h name - - - - - - epc1h epc1l b7 ~ b2 : reserved epc1h : associated with ccap1h, it is used in pca pwm mode. epc1l : associated with ccap1l, it is used in pca pwm mode. 7. register swicthing pca/pwm function from p1 port to p4 port : auxr1 sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 reset value auxr1 a2h name - pca_p4 spi_p4 s2_p4 gf2 adrj - dps x000,00x0 pca _p4 0 : default. the pca function is on p1[4:2] 1 : the pca function on p1[4:2] is switched to p4[3:1]. eci is switched from p1.2 to p4.1 pca0/pwm0 is switched from p1.3 to p4.2 pca1/pwm1 is switched from p1.4 to p4.3 spi_p4 0 : default. the spi function is on p1[7:4] 1 : the spi function on p1[7:4] is switched to p4[3:0]. sclk is switched from p1.7 to p4.3 mosi is switched from p1.6 to p4.2 miso is switched from p1.5 to p4.1 ss is switched from p1.4 to p4.0 s2_p4 0 : default. the uart2(s2) function is on p1[3:2] 1 : the uart2(s2) function on p1[3:2] is switched to p4[3:2]. txd2 is switched from p1.3 to p4.3 rxd2 is switched from p1.2 to p4.2 gf2 : general flag. it can be used by software. adrj 0 : the 10-bit conversion result of adc is arranged as {adc_res[7:0], adc_resl[1:0]}. 1 : the 10-bit conversion result is right-justified, {adc_res[1:0], adc_resl[7:0]}. dps 0 : default. dptr0 is selected as data pointer. 1 : the secondary dptr is switched to use. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 296 www.stcmcu.com 16-bit pca timer/counter module 0 module 1 p4.2/ccp0/pca0/pwm0 (auxr1.6/pca_p4=1) p1.3/ccp0/pca0/pwm0 (auxr1.6/pca_p4=0) p4.3/ccp1/pca1/pwm1 (auxr1.6/pca_p4=1) p1.4/ccp1/pca1/pwm1 (auxr1.6/pca_p4=0) programmable counter arrary structure 10.2 pca/pwm structure ccon to pca module ch cl 16-bit counter pca interrput sysclk/1 sysclk/2 sysclk/4 sysclk/6 sysclk/8 sysclk/12 timer 0 overflow external input eci(p1.2) idle pca timer/counter cf cr - - - - ccf1 ccf0 cidl - - - cps2 cps1 cps0 ecf cmod there are 2 channels programmable counter arrary pca/pwm in stc12c5a60s2 series mcu. (pca/pwm function can be swicthed from p1 port to p4 port by setting auxr1 register) each pca/pwm module can be operated in 4 modes : rising / falling capture mode, software timer, high-speed output mode and adjustable pulse output mode. stc12c5a60s2 series: module 0 connect to p1.3/ccp0 (which can be swiched to p4.2/ccp0/miso), module 1 connect to p1.4/ccp1 (which can be swiched to p4.3/ccp1/sclk). stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 297 www.stcmcu.com in the cmod sfr, there are two additional bits associated with the pca. they are cidl which allows the pca to stop during idle mode, and ecf which when set causes an interrupt and the pca overflow flag cf(in the ccon sfr) to be set when the pca timer overflows. the ccon sfr contains the run control bit for pca and the flags for the pca timer and each module. to run the pca the cr bit(ccon.6) must be set by software; oppositely clearing bit cr will shut off pca is shut off pca. the cf bit(ccon.7) is set when the pca counter overflows and an interrupt will be generated if the ecf (cmod.0) bit in the cmod register is set. the cf bit can only be cleared by software. there are two bits named ccf0 and ccf1 in sfr ccon. the ccf0 and ccf1 are the flags for module 0 and module 1 respectively. they are set by hardware when either a match or a capture occurs. these flags also can only be cleared by software. each module in the pca has a special function register associated with it, ccapm0 for module-0 and ccapm1 for module-1. the register contains the bits that control the mode in which each module will operate. the eccfn bit controls if to pass the interrupt from ccfn flag in the ccon sfr to the mcu when a match or compare occurs in the associated module. pwmn enables the pulse width modulation mode. the togn bit when set causes the pin ccpn output associated with the module to toggle when there is a match between the pca counter and the modules capture/compare register. the match bit(matn) when set will cause the ccfn bit in the ccon register to be set when there is a match between the pca counter and the modules capture/compare register. the next two bits capnn and cappn determine the edge type that a capture input will be active on. the capnn bit enables the negative edge, and the cappn bit enables the positive edge. if both bits are set, both edges will be enabled and a capture will occur for either transition. the bit ecomn when set enables the comparator function. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 298 www.stcmcu.com 10.3 pca modules operation mode the operation mode of pca modules set as shown in the below table. setting the operation mode of pca modules ccapmn register n = 0,1 - ecomn cappn capnn matn togn pwmn eccfn function of pca modules 0 0 0 0 0 0 0 no operation 1 0 0 0 0 1 0 8-bit pwm, no interrupt 1 1 0 0 0 1 1 8-bit pwm output, interrupt can be generated on rising edge. 1 0 1 0 0 1 1 8-bit pwm output, interrupt can be generated on falling edge. 1 1 1 0 0 1 1 8-bit pwm output, interrupt can be generated on both rising and falling edges. x 1 0 0 0 0 x 16-bit capture mode, caputre triggered by the rising edge on ccpn/pcan pin x 0 1 0 0 0 x 16-bit capture mode, capture triggered by the falling edge on ccpn/pcan pin x 1 1 0 0 0 x 16-bit capture mode, capture triggered by the transition on ccpn/pcan pin 1 0 0 1 0 0 x 16-bit software timer 1 0 0 1 1 0 x 16-bit high-speed output 10.3.1 pca capture mode to use one of the pca modules in the capture mode either one or both of the ccapm bits C cappn and capnn, for the module must be set. the external ccpn input (ccp0/p1.3, ccp1/p1.4 or ccp0/ p4.2, ccp1/p4.3) for the module is sampled for a transition. when a valid transition occurs, the pca hardware loads the value of the pca counter register(ch and cl) into the modules capture registers(ccapnh and ccapnl). if the ccfn bit for the module in the ccon sfr and the eccfn bit in the ccapmn sfr are set then an interrupt will be generated. cf cr ccf1 ccf0 ecomn cappn capnn matn togn pwmn eccfn ch cl ccapnh ccapnl ccpn 0 0 0 0 capture pca interrupt ccon (address:d8h) ccapmn, n=0,1 address: dah dbh pca capture mode (ccp0/p1.3, ccp1.p1.4) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 299 www.stcmcu.com 10.3.2 16-bit software timer mode the pca modules can be used as software timers by setting both the ecomn and matn bits in the modules ccapmn register. the pca timer will be compared to the modules capture registers and when a match occurs an interrupt will be generated if the ccfn and eccfn bits for the module are both set. write first write to ccapnl write late write to ccapnh ccon ecomn cappn eccfn - pwmn capnn matn togn ccapmn ccapnh ccapnl 16-bit comparator enable match ch cl (to ccfn) pca interrupt 0 1 0 0 1 0 0 pca software timer mode cf cr ccf1 ccf0 stop comparing renew comparing if ecomn=0, stop comparing if ecomn=0, renew comparing stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 300 www.stcmcu.com 10.3.3 high speed output mode in this mode the ccpn output (port latch) associated with the pca module will toggle each time a match occurs between the pca counter and the modules capture registers. to activate this mode the togn,matn,and ecomn bits in the modules ccapmn sfr must be set. pca high-speed output mode write first write to ccapnl write late write to ccapnh ccon ecomn cappn eccfn - pwmn capnn matn togn ccapmn ccapnh ccapnl 16-bit comparator enable match ch cl (to ccfn) pca interrupt 0 1 0 1 1 0 0 cf cr ccf1 ccf0 stop comparing renew comparing if ecomn=0, stop comparing if ecomn=0, renew comparing toggle ccpn stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 301 www.stcmcu.com 10.3.4 pulse width modulator mode (pwm mode) all of the pca modules can be used as pwm outputs. the frequency of the output depends on the source for the pca timer. all of the modules will have the same frequency of output because they all share the same pca timer. the duty cycle of each module is independently variable using the module s capture register ccapnl and epcnl bit in pcapwmn register. when the value of the pca cl sfr is less than the value in the modules {epcnl,ccapnl} sfr, the output will be low. when it is equal to or greater than , the output will be high. when cl overflows from ffh to 00h, {epcnl,ccapnl} is reloaded with the value in {epcnh,ccapnh}. that allows updating the pwm without glitches. the pwmn and ecomn bits in the modules ccapmn register must be set to enable the pwm mode. ccapnh epcnh ccapnl epcnl cl (0,cl)<(epcnl,ccpnl) (0,cl)>=(epcnl,ccpnl) output 0 pwmn ccpn enable capnn 0 cl overflow - ecomn cappn matn togn pwmn eccfn ccapmn, n=0,1 9-bit comparator 1 0 0 0 1 0 pca pwm mode 0 output 1 when cps2 / cps1 / cps0 = 1/0/0, pca, pwm clock source is sysc1k. disable timer 0, the frequency of pwm is sysc1k / 256. if the system clock/3 is need to be used as pca clock source, t0 should be enabled in 1t mode and spill after counting three pulse. and internal r/c oscillator is used as system clock, which can output pwm with 14khz to 19khz frequency. t0's overflow can divide the frequency of system clock for 1 ~ 256 levels. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 302 www.stcmcu.com 10.4 programs for pca module extended external interrupt (c and asm) there are two programs for pca module extended external interrupt demo, one wrriten in c language and the other in assembly language. 1. c code listing: /*-------------------------------------------------------------------------------------*/ /* --- stc mcu international limited ----------------------------------------*/ /* --- stc 1t series mcu pca module extended external interrupt ----*/ /* --- mobile: (86)13922809991 -----------------------------------------------*/ /* --- fax: 86-755-82905966 ---------------------------------------------------*/ /* --- tel: 86-755-82948412 ----------------------------------------------------*/ /* --- web: www.stcmcu.com -----------------------------------------------*/ /* if you want to use the program or the program referenced in the */ /* article, please specify in which data and procedures from stc */ /*------------------------------------------------------------------------------------*/ #include "reg51.h" #include "intrins.h" typedef unsigned char byte; typedef unsigned int word; /*declare sfr associated with the pca */ sfr ccon = 0xd8; //pca control register sbit ccf0 = ccon^0; //pca module-0 interrupt flag sbit ccf1 = ccon^1; //pca module-1 interrupt flag sbit cr = ccon^6; //pca timer run control bit sbit cf = ccon^7; //pca timer overflow flag sfr cmod = 0xd9; //pca mode register sfr cl = 0xe9; //pca base timer low sfr ch = 0xf9; //pca base timer high sfr ccapm0= 0xda; //pca module-0 mode register sfr ccap0l = 0xea; //pca module-0 capture register low sfr ccap0h = 0xfa; //pca module-0 capture register high sfr ccapm1= 0xdb; //pca module-1 mode register sfr ccap1l = 0xeb; //pca module-1 capture register low sfr ccap1h = 0xfb; //pca module-1 capture register high sfr pcapwm0=0xf2; sfr pcapwm1=0xf3; sbit pca_led =p1^0; //pca test led void pca_isr() interrupt 7 using 1 { ccf0 = 0; //clear interrupt flag pca_led = !pca_led; //toggle the test pin while ccp0(p1.3) have a falling edge } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 303 www.stcmcu.com 2. assembly code listing: ;/*------------------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited ---------------------------------------------*/ ;/* --- stc 1t series mcu pca module extended external interrupt ----------*/ ;/* --- mobile: (86)13922809991 -----------------------------------------------------*/ ;/* --- fax: 86-755-82905966 ---------------------------------------------------------*/ ;/* --- tel: 86-755-82948412 ----------------------------------------------------------*/ ;/* --- web: www.stcmcu.com -----------------------------------------------------*/ ;/* if you want to use the program or the program referenced in the */ ;/* article, please specify in which data and procedures from stc */ ;/*-------------------------------------------------------------------------------------------*/ ;/*declare sfr associated with the pca */ ccon equ 0d8h ;pca control register ccf0 bit ccon.0 ;pca module-0 interrupt flag ccf1 bit ccon.1 ;pca module-1 interrupt flag cr bit ccon.6 ;pca timer run control bit cf bit ccon.7 ;pca timer overflow flag cmod equ 0d9h ;pca mode register cl equ 0e9h ;pca base timer low ch equ 0f9h ;pca base timer high void main() { ccon = 0; //initial pca control register //pca timer stop running //clear cf flag //clear all module interrupt flag cl = 0; //reset pca base timer ch = 0; cmod = 0x00; //set pca timer clock source as fosc/12 //disable pca timer overflow interrupt ccapm0 = 0x11; //pca module-0 capture by a negative tigger on ccp0(p1.3) //and enable pca interrupt // ccapm0 = 0x21; //pca module-0 capture by a rising edge on ccp0(p1.3) //and enable pca interrupt // ccapm0 = 0x31; //pca module-0 capture by a transition (falling/rising edge) //on ccp0(p1.3) and enable pca interrupt cr = 1; //pca timer start run ea = 1; while (1); } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 304 www.stcmcu.com ccapm0 equ 0dah ;pca module-0 mode register ccap0l equ 0eah ;pca module-0 capture register low ccap0h equ 0fah ;pca module-0 capture register high ccapm1 equ 0dbh ;pca module-1 mode register ccap1l equ 0ebh ;pca module-1 capture register low ccap1h equ 0fbh ;pca module-1 capture register high pca_led bit p1.0 ;pca test led ;------------------------------------------------------------------------------------------------ org 0000h ljmp main org 003bh pca_isr: clr ccf0 ;clear interrupt flag cpl pca_led ;toggle the test pin while ccp0(p1.3) have a falling edge reti ;------------------------------------------------------------------------------------------------ org 0100 main: mov ccon, #0 ;initial pca control register ;pca timer stop running ;clear cf flag ;clear all module interrupt flag clr a ; mov cl, a ;reset pca base timer mov ch, a ; mov cmod, #00h ;set pca timer clock source as fosc/12 ;disable pca timer overflow interrupt mov ccapm0, #11h ;pca module-0 capture by a falling edge on ccp0(p1.3) ;and enable pca interrupt ; mov ccapm0, #21h ;pca module-0 capture by a rising edge on ccp0(p1.3) ;and enable pca interrupt ; mov ccapm0, #31h ;pca module-0 capture by a transition (falling/rising edge) ;on ccp0(p1.3) and enable pca interrupt ;------------------------------- setb cr ;pca timer start run setb ea sjmp $ ;----------------------------------------- end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 305 www.stcmcu.com 10.5 demo programs for pca module acted as 16-bit timer (c and asm) there are two programs for pca module acted as 16-bit timer demo, one wrriten in c language and the other in assembly language. 1. c code listing: ;/*------------------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited ---------------------------------------------*/ ;/* --- stc 1t series mcu pca module acted as 16-bit timer demo ---------*/ ;/* --- mobile: (86)13922809991 -----------------------------------------------------*/ ;/* --- fax: 86-755-82905966 ---------------------------------------------------------*/ ;/* --- tel: 86-755-82948412 ----------------------------------------------------------*/ ;/* --- web: www.stcmcu.com -----------------------------------------------------*/ ;/* if you want to use the program or the program referenced in the */ ;/* article, please specify in which data and procedures from stc */ ;/*------------------------------------------------------------------------------------------*/ #include "reg51.h" #include "intrins.h" #define fosc 18432000l #define t100hz (fosc / 12 / 100) typedef unsigned char byte; typedef unsigned int word; /*declare sfr associated with the pca */ sfr ccon = 0xd8; //pca control register sbit ccf0 = ccon^0; //pca module-0 interrupt flag sbit ccf1 = ccon^1; //pca module-1 interrupt flag sbit cr = ccon^6; //pca timer run control bit sbit cf = ccon^7; //pca timer overflow flag sfr cmod = 0xd9; //pca mode register sfr cl = 0xe9; //pca base timer low sfr ch = 0xf9; //pca base timer high sfr ccapm0= 0xda; //pca module-0 mode register sfr ccap0l = 0xea; //pca module-0 capture register low sfr ccap0h = 0xfa; //pca module-0 capture register high sfr ccapm1= 0xdb; //pca module-1 mode register sfr ccap1l = 0xeb; //pca module-1 capture register low sfr ccap1h = 0xfb; //pca module-1 capture register high sfr pcapwm0=0xf2; sfr pcapwm1=0xf3; sbit pca_led = p1^0; //pca test led byte cnt; word value; stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 306 www.stcmcu.com void pca_isr() interrupt 7 using 1 { ccf0 = 0; //clear interrupt flag ccap0l = value; ccap0h = value >> 8; //update compare value value += t100hz; if (cnt-- == 0) { cnt = 100; //count 100 times pca_led = !pca_led; //flash once per second } } void main() { ccon = 0; //initial pca control register //pca timer stop running //clear cf flag //clear all module interrupt flag cl = 0; //reset pca base timer ch = 0; cmod = 0x00; //set pca timer clock source as fosc/12 //disable pca timer overflow interrupt value = t100hz; ccap0l = value; ccap0h = value >> 8; //initial pca module-0 value += t100hz; ccapm0 = 0x49; //pca module-0 work in 16-bit timer mode //and enable pca interrupt cr = 1; //pca timer start run ea = 1; cnt = 0; while (1); } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 307 www.stcmcu.com 2. assembly code listing: ;/*------------------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited ---------------------------------------------*/ ;/* --- stc 1t series mcu pca module acted as 16-bit timer demo ---------*/ ;/* --- mobile: (86)13922809991 -----------------------------------------------------*/ ;/* --- fax: 86-755-82905966 ---------------------------------------------------------*/ ;/* --- tel: 86-755-82948412 ---------------------------------------------------------*/ ;/* --- web: www.stcmcu.com ----------------------------------------------------*/ ;/* if you want to use the program or the program referenced in the */ ;/* article, please specify in which data and procedures from stc */ ;/*-----------------------------------------------------------------------------------------*/ t100hz equ 3c00h ;(18432000 / 12 / 100) ;/*declare sfr associated with the pca */ ccon equ 0d8h ;pca control register ccf0 bit ccon.0 ;pca module-0 interrupt flag ccf1 bit ccon.1 ;pca module-1 interrupt flag cr bit ccon.6 ;pca timer run control bit cf bit ccon.7 ;pca timer overflow flag cmod equ 0d9h ;pca mode register cl equ 0e9h ;pca base timer low ch equ 0f9h ;pca base timer high ccapm0 equ 0dah ;pca module-0 mode register ccap0l equ 0eah ;pca module-0 capture register low ccap0h equ 0fah ;pca module-0 capture register high ccapm1 equ 0dbh ;pca module-1 mode register ccap1l equ 0ebh ;pca module-1 capture register low ccap1h equ 0fbh ;pca module-1 capture register high pca_led bit p1.0 ;pca test led cnt equ 20h ;------------------------------------------------------------------------ org 0000h ljmp main org 003bh ljmp pca_isr ;-------------------------------------------------------------------------- org 0100h main: mov sp, #3fh ;initial stack point mov ccon ,#0 ;initial pca control register ;pca timer stop running ;clear cf flag ;clear all module interrupt flag stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 308 www.stcmcu.com clr a ; mov cl, a ;reset pca base timer mov ch, a ; mov cmod, #00h ;set pca timer clock source as fosc/12 ;disable pca timer overflow interrupt ;----------------------------------------------------------------------------- mov ccap0l, #low t100hz ; mov ccap0h, #high t100hz ;initial pca module-0 mov ccapm0, #49h ;pca module-0 work in 16-bit timer mode ;and enable pca interrupt ;------------------------------------------------------------------------------- setb cr ;pca timer start run setb ea mov cnt, #100 sjmp $ ;--------------------------------------------------------------------------------- pca_isr: push psw push acc clr ccf0 ;clear interrupt flag mov a, ccap0l add a, #low t100hz ;update compare value mov ccap0l, a mov a, ccap0h addc a, #high t100hz mov ccap0h, a djnz cnt, pca_isr_exit ;count 100 times mov cnt, #100 cpl pca_led ;flash once per second pca_isr_exit: pop acc pop psw reti ;------------------------------------------------------------------------------------ end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 309 www.stcmcu.com 10.6 programs for pca module as 16-bit high speed output(c and asm) there are two programs for pca module as 16-bit high speed output, one wrriten in c language and the other in assembly language. 1. c code listing: ;/*--------------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited -----------------------------------------*/ ;/* --- stc 1t series mcu pca module as 16-bit high speed output ----*/ ;/* --- mobile: (86)13922809991 -------------------------------------------------*/ ;/* --- fax: 86-755-82905966 -----------------------------------------------------*/ ;/* --- tel: 86-755-82948412 ------------------------------------------------------*/ ;/* --- web: www.stcmcu.com -------------------------------------------------*/ ;/* if you want to use the program or the program referenced in the */ ;/* article, please specify in which data and procedures from stc */ ;/*---------------------------------------------------------------------------------------*/ #include "reg51.h" #include "intrins.h" #define fosc 18432000l #define t100khz (fosc / 4 / 100000) typedef unsigned char byte; typedef unsigned int word; /*declare sfr associated with the pca */ sfr ccon = 0xd8; //pca control register sbit ccf0 = ccon^0; //pca module-0 interrupt flag sbit ccf1 = ccon^1; //pca module-1 interrupt flag sbit cr = ccon^6; //pca timer run control bit sbit cf = ccon^7; //pca timer overflow flag sfr cmod = 0xd9; //pca mode register sfr cl = 0xe9; //pca base timer low sfr ch = 0xf9; //pca base timer high sfr ccapm0 = 0xda; //pca module-0 mode register sfr ccap0l = 0xea; //pca module-0 capture register low sfr ccap0h = 0xfa; //pca module-0 capture register high sfr ccapm1 = 0xdb; //pca module-1 mode register sfr ccap1l = 0xeb; //pca module-1 capture register low sfr ccap1h = 0xfb; //pca module-1 capture register high sfr pcapwm0 = 0xf2; sfr pcapwm1 = 0xf3; sbit pca_led = p1^0; //pca test led byte cnt; word value; stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 310 www.stcmcu.com void pca_isr( ) interrupt 7 using 1 { ccf0 = 0; //clear interrupt flag ccap0l = value; ccap0h = value >> 8; //update compare value value += t100khz; } void main() { ccon = 0; //initial pca control register //pca timer stop running //clear cf flag //clear all module interrupt flag cl = 0; //reset pca base timer ch = 0; cmod = 0x02; //set pca timer clock source as fosc/2 //disable pca timer overflow interrupt value = t100khz; ccap0l = value; //p1.3 output 100khz square wave ccap0h = value >> 8; //initial pca module-0 value += t100khz; ccapm0 = 0x4d; //pca module-0 work in 16-bit timer mode //and enable pca interrupt, toggle the output pin ccp0(p1.3) cr = 1; //pca timer start run ea = 1; cnt = 0; while (1); } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 311 www.stcmcu.com 2. assembly code listing: ;/*--------------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited -----------------------------------------*/ ;/* --- stc 1t series mcu pca module as 16-bit high speed output ----*/ ;/* --- mobile: (86)13922809991 -------------------------------------------------*/ ;/* --- fax: 86-755-82905966 -----------------------------------------------------*/ ;/* --- tel: 86-755-82948412 ------------------------------------------------------*/ ;/* --- web: www.stcmcu.com -------------------------------------------------*/ ;/* if you want to use the program or the program referenced in the */ ;/* article, please specify in which data and procedures from stc */ ;/*---------------------------------------------------------------------------------------*/ t100khz equ 2eh ;(18432000 / 4 / 100000) ;/*declare sfr associated with the pca */ ccon equ 0d8h ;pca control register ccf0 bit ccon.0 ;pca module-0 interrupt flag ccf1 bit ccon.1 ;pca module-1 interrupt flag cr bit ccon.6 ;pca timer run control bit cf bit ccon.7 ;pca timer overflow flag cmod equ 0d9h ;pca mode register cl equ 0e9h ;pca base timer low ch equ 0f9h ;pca base timer high ccapm0 equ 0dah ;pca module-0 mode register ccap0l equ 0eah ;pca module-0 capture register low ccap0h equ 0fah ;pca module-0 capture register high ccapm1 equ 0dbh ;pca module-1 mode register ccap1l equ 0ebh ;pca module-1 capture register low ccap1h equ 0fbh ;pca module-1 capture register high ;----------------------------------------- org 0000h ljmp main org 003bh pca_isr: push psw push acc clr ccf0 ;clear interrupt flag mov a, ccap0l add a, #t100khz ;update compare value mov ccap0l, a clr a addc a, ccap0h stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 312 www.stcmcu.com mov ccap0h, a pca_isr_exit: pop acc pop psw reti ;----------------------------------------------------------------------- org 0100h main: mov ccon, #0 ;initial pca control register ;pca timer stop running ;clear cf flag ;clear all module interrupt flag clr a ; mov cl, a ;reset pca base timer mov ch, a ; mov cmod, #02h ;set pca timer clock source as fosc/2 ;disable pca timer overflow interrupt ;-------------------------------------------------------------------------- mov ccap0l, #t100khz ;p1.3 output 100khz square wave mov ccap0h,#0 ;initial pca module-0 mov ccapm0,#4dh ;pca module-0 work in 16-bit timer mode and enable ;pca interrupt, toggle the output pin cex0(p1.3) ;--------------------------------------------------------------------------- setb cr ;pca timer start run setb ea sjmp $ ;----------------------------------------------------------------------------- end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 313 www.stcmcu.com 10.7 demo programs for pca module as pwm output (c and asm) 1. c code listing: *------------------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited --------------------------------------------*/ ;/* --- stc 1t series mcu pca module output pwm wave demo -----------*/ ;/* --- mobile: (86)13922809991 ----------------------------------------------------*/ ;/* --- fax: 86-755-82905966 --------------------------------------------------------*/ ;/* --- tel: 86-755-82948412 ---------------------------------------------------------*/ ;/* --- web: www.stcmcu.com ----------------------------------------------------*/ ;/* if you want to use the program or the program referenced in the */ ;/* article, please specify in which data and procedures from stc */ ;/*------------------------------------------------------------------------------------------*/ #include "reg51.h" #include "intrins.h" #define fosc 18432000l typedef unsigned char byte; typedef unsigned int word; / *declare sfr associated with the pca */ sfr ccon = 0xd8; //pca control register sbit ccf0 = ccon^0; //pca module-0 interrupt flag sbit ccf1 = ccon^1; //pca module-1 interrupt flag sbit cr = ccon^6; //pca timer run control bit sbit cf = ccon^7; //pca timer overflow flag sfr cmod = 0xd9; //pca mode register sfr cl = 0xe9; //pca base timer low sfr ch = 0xf9; //pca base timer high sfr ccapm0= 0xda; //pca module-0 mode register sfr ccap0l = 0xea; //pca module-0 capture register low sfr ccap0h = 0xfa; //pca module-0 capture register high sfr ccapm1= 0xdb; //pca module-1 mode register sfr ccap1l = 0xeb; //pca module-1 capture register low sfr ccap1h = 0xfb; //pca module-1 capture register high sfr pcapwm0=0xf2; sfr pcapwm1=0xf3; void main() { ccon = 0; //initial pca control register //pca timer stop running //clear cf flag //clear all module interrupt flag stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 314 www.stcmcu.com 2. assembly code listing: *------------------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited --------------------------------------------*/ ;/* --- stc 1t series mcu pca module output pwm wave demo -----------*/ ;/* --- mobile: (86)13922809991 ----------------------------------------------------*/ ;/* --- fax: 86-755-82905966 --------------------------------------------------------*/ ;/* --- tel: 86-755-82948412 ---------------------------------------------------------*/ ;/* --- web: www.stcmcu.com ----------------------------------------------------*/ ;/* if you want to use the program or the program referenced in the */ ;/* article, please specify in which data and procedures from stc */ ;/*------------------------------------------------------------------------------------------*/ ;/*declare sfr associated with the pca */ ccon equ 0d8h ;pca control register ccf0 bit ccon.0 ;pca module-0 interrupt flag ccf1 bit ccon.1 ;pca module-1 interrupt flag cr bit ccon.6 ;pca timer run control bit cf bit ccon.7 ;pca timer overflow flag cmod equ 0d9h ;pca mode register cl equ 0e9h ;pca base timer low ch equ 0f9h ;pca base timer high ccapm0 equ 0dah ;pca module-0 mode register ccap0l equ 0eah ;pca module-0 capture register low ccap0h equ 0fah ;pca module-0 capture register high ccapm1 equ 0dbh ;pca module-1 mode register ccap1l equ 0ebh ;pca module-1 capture register low ccap1h equ 0fbh ;pca module-1 capture register high cl = 0; //reset pca base timer ch = 0; cmod = 0x02; //set pca timer clock source as fosc/2 //disable pca timer overflow interrupt ccap0h = ccap0l = 0x80; //pwm0 port output 50% duty cycle square wave ccapm0 = 0x42; //pca module-0 work in 8-bit pwm mode //and no pca interrupt ccap1h = ccap1l = 0xff; //pwm1 port output 0% duty cycle square wave pcapwm1 = 0x03; ccapm1 = 0x42; //pca module-1 work in 8-bit pwm mode //and no pca interrupt cr = 1; //pca timer start run while (1); } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 315 www.stcmcu.com org 0000h ljmp main org 0100h main: mov ccon, #0 ;initial pca control register ;pca timer stop running ;clear cf flag ;clear all module interrupt flag clr a ;reset pca base timer mov cl, a ; mov ch, a ; mov cmod, #02h ;set pca timer clock source as fosc/2 ;disable pca timer overflow interrupt mov a, #080h ;pwm0 port output 50% duty cycle square wave mov ccap0h, a ; mov ccap0l, a ; mov ccapm0, #42h ;pca module-0 work in 8-bit pwm mode ;and no pca interrupt mov a, #0c0h ;pwm1 port output 25% duty cycle square wave mov ccap1h,a ; mov ccap1l, a ; mov ccapm1,#42h ;pca module-1 work in 8-bit pwm mode ;and no pca interrupt setb cr ;pca timer start run sjmp $ end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 316 www.stcmcu.com 10.8 demo program for pca clock base on timer 1 overflow rate ;/*-------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited ----------------------------------*/ ;/* --- stc 1t series achieve adjustable frequency pwm output-----*/ ;/* --- mobile: (86)13922809991 ------------------------------------------*/ ;/* --- fax: 86-755-82905966 ----------------------------------------------*/ ;/* --- tel: 86-755-82948412 -----------------------------------------------*/ ;/* --- web: www.stcmcu.com -----------------------------------------*/ ;/* if you want to use the program or the program referenced in the */ ;/* article, please specify in which data and procedures from stc */ ;/*------------------------------------------------------------------------------*/ ;/*declare sfr associated with the adc */ iph equ 0b7h ;interrupt priority control register high byte ccon equ 0d8h ;pca control register ccf0 bit ccon.0 ;pca module-0 interrupt flag ccf1 bit ccon.1 ;pca module-1 interrupt flag cr bit ccon.6 ;pca timer run control bit cf bit ccon.7 ;pca timer overflow flag cmod equ 0d9h ;pca mode register cl equ 0e9h ;pca base timer low ch equ 0f9h ;pca base timer high ccapm0 equ 0dah ;pca module-0 mode register ccap0l equ 0eah ;pca module-0 capture register low ccap0h equ 0fah ;pca module-0 capture register high ccapm1 equ 0dbh ;pca module-1 mode register ccap1l equ 0ebh ;pca module-1 capture register low ccap1h equ 0fbh ;pca module-1 capture register high ;/*define working led */ led_mcu_start equ p1.7 led_5ms_flashing equ p1.6 led_1s_flashing equ p1.5 channel_5ms_h equ 01h ;pca module-1 5ms counter high byte @ 18.432mhz channel_5ms_l equ 00h ;pca module-1 5ms counter low byte @ 18.432mhz timer0_reload_1 equ 0f6h ;timer0 auto reload value (-10) timer0_reload_2 equ 0ech ;timer0 auto reload value (-20) pwm_width equ 0ffh ;low duty counter equ 030h ;counter value stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 317 www.stcmcu.com ;----------------------------------------- org 0000h ljmp main org 003bh ljmp pca_interrupt ;----------------------------------------- org 0050h main: clr led_mcu_start ;turn on mcu working led mov sp,#7fh mov counter,#0 ;initial counter var acall pac_initial ;initial pca acall timer0_initial ;initial timer0 main_loop: ;------------------------------- mov th0,#timer0_reload_1 ;set timer0 overload rate 1 mov tl0,#timer0_reload_1 mov a,#pwm_width ;setting duty mov ccap0h,a acall delay mov th0,#timer0_reload_2 ;set timer0 overload rate 2 mov tl0,#timer0_reload_2 acall delay ;------------------------------- mov th0,#timer0_reload_1 ;set timer0 overload rate 1 mov tl0,#timer0_reload_1 mov a,#pwm_width acall rl_a ;change duty acall rl_a mov ccap0h,a acall delay mov th0,#timer0_reload_2 ;set timer0 overload rate 2 mov tl0,#timer0_reload_2 acall delay ;------------------------------- mov th0,#timer0_reload_1 ;set timer0 overload rate 1 mov tl0,#timer0_reload_1 mov a,#pwm_width acall rl_a ;change duty stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 318 www.stcmcu.com acall rl_a acall rl_a acall rl_a mov ccap0h,a acall delay mov th0,#timer0_reload_2 ;set timer0 overload rate 2 mov tl0,#timer0_reload_2 acall delay ;------------------------------- sjmp main_loop ;----------------------------------------- rl_a: clr c rrc a ret ;----------------------------------------- timer0_initial: mov tmod,#02h ;8-bit auto-reload mov th0,#timer0_reload_1 mov tl0,#timer0_reload_1 setb tr0 ;strat run ret ;----------------------------------------- pca_initial: mov cmod,#10000100b ;pca timer base on timer0 mov ccon,#00h ;pca stop count mov cl,#0 ;initial pca counter mov ch,#0 mov ccapm0,#42h ;pca module-0 as 8-bit pwm mov pca_pwm0,#0 ;pwm mode 9 th bit ; mov pca_pwm0,#03h ;pwm will keep low level mov ccap1l,#channel_5ms_l ;initial pca module-1 mov ccap1h,#channel_5ms_h mov ccapm1,#49h ;pca module-1 as 16-bit timer setb ea setb cr ;pca counter start running ret stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 319 www.stcmcu.com ;----------------------------------------- pca_interrupt: push acc push psw cpl led_5ms_flashing ;flashing once per 5ms mov a,#channel_5ms_l add a,ccap1l mov ccap1l,a mov a,#channel_5ms_h addc a,ccap1h mov ccap1h,a clr ccf1 ;clear pca module-1 flag inc counter mov a,counter clr c subb a,#100 ;count 100 times jc pca_interrupt_exit mov counter,#0 cpl led_1s,flash pca_interrupt_exit: pop psw pop acc reti ;----------------------------------------- delay: clr a mov r1,a mov r2,a mov r3,#80h delay_loop: nop nop nop djnz r1,delay_loop djnz r2,delay_loop djnz r3,delay_loop ret ;----------------------------------------- end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 320 www.stcmcu.com 10.9 using pwm achieve d/a conversion function reference circuit 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 sop-32 p2.2 p2.3 rst rxd/p3.0 txd/p3.1 p0.0 xtal2 xtal1 int0/p3.2 p0.1 int1/p3.3 clkout0/eci/t0/p3.4 p2.4 p2.5 vss clkout1/pwm1/t1/p3.5 vdd p2.1 p2.0 p1.7/adc7 p1.6/adc6 p1.5/adc5 p0.3 p1.4/adc4 p1.3/adc3 p0.2 p1.2/adc2/lvd p1.1/adc1 p1.0/adc0 p3.7/pwm0 p2.7 p2.6 10k 10k d/a 104 104 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 321 www.stcmcu.com chapter 11. serial peripheral interface (spi) stc12c5a60s2 p.rovides another high-speed serial communication interface, the spi interface. spi is a full-duplex, high-speed, synchronous communication bus with two operation modes: master mode and slave mode. up to 3mbit/s can be supported in either master or slave mode under the sysclk=12mhz. two status flags are provided to signal the transfer completion and write-collision occurrence. 1. spi control register: spctl (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 spctl ceh name ssig spen dord mstr cpol cpha spr1 spr0 ssig : control whether ss pin is ignored or not. if ssig=1, mstr(spctl.4) decides whether the device is a master or slave. if ssig=0, the ss pin decides whether the device is a master or slave. ss pin can be used as i/o port. spen : spi enable bit. if spen=0, the spi interface is disabled and all spi pins will be general-purpose i/o ports. if spen=1, the spi is enabled. dord : set the transmitted or received spi data order. if dord=1, the lsb of the data word is transmitted first. if dord=0, the msb of the data word is transmitted first. mstr : master/slave mode select bit. if mstr=0, set the spi to play as slave part. if mstr=1, set the spi to play as master part. cpol : spi clock polarity select bit. if cpol=1, spiclk is high level when in idle mode. the leading edge of spiclk is the falling edge and the trailing edge is the rising edge. if cpol=0, spiclk is low when idle. the leading edge of spiclk is the rising edge and the trailing edge is the falling edge. cpha : spi clock phase select bit. if cpha=1, data is driven on the leading edge of spiclk, and is sampled on the trailing edge. if cpha=0, data is driven when ss pin is low (ssig=0) and changes on the trailing edge of spiclk. data is sampled on the leading edge of spiclk. (note : if ssig=1, cpha must not be 0, otherwise the operation is undefined) 11.1 special function registers related with spi mnemonic description address bit address and symbol reset value b7 b6 b5 b4 b3 b2 b1 b0 spctl spi control register ceh ssig spen dord mstr cpol cpha spr1 spr0 0000,0100 spstat spi status register cdh spif wcol - - - - - - 00xx,xxxx spdat spi data register cfh 0000,0000 auxr1 auxiliary register 1 a2h - pca_p4 spi_p4 s2_p4 gf2 adrj - dps x000,00x0 spi management sfrs stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 322 www.stcmcu.com spr1-spr0 : spi clock rate select bit (when in master mode) spi clock frequency select bit spr1 spr0 spi clock (sclk ) 0 0 cpu_clk/4 0 1 cpu_clk/16 1 0 cpu_clk/64 1 1 cpu_clk/128 cpu_clk is cpu clock. when cpha equals 0, ssig must be 0 and ss pin must be negated and reasserted between each successive serial byte transfer. if the spdat register is written while ss is active(0), a write collision error results and wcol is set. when cpha equals 1, ssig may be 0 or 1. if ssig=0, the ss pin may remain active low between successive transfers(can be tied low at any times). this format is sometimes preferred for use in systems having a signle fixed master and a single slave configuration. 2. spi state register: spstat (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 spstat cdh name spif wcol - - - - - - spif : spi transfer completion flag. when a serial transfer finishes, the spif bit is set and an interrupt is generated if both the espi (ie.6) bit and the ea (ie.7) bit are set. if ss is an input and is driven low when spi is in master mode with ssig = 0, spif will also be set to signal the mode change.the spif is cleared in software by writing 1 to this bit. wcol: spi write collision flag. the wcol bit is set if the spi data register, spdat, is written during a data transfer. the wcol flag is cleared in software by writing 1 to this bit. 3. spi data register : spdat (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 spdat cfh name the sfr spdat holds the data to be transmitted or the data received. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 323 www.stcmcu.com 4. register swicthing spi function from p1 port to p4 port : auxr1 (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 reset value auxr1 a2h name - pca_p4 spi_p4 s2_p4 gf2 adrj - dps x000,00x0 pca _p4 0 : default. the pca function is on p1[4:2] 1 : the pca function on p1[4:2] is switched to p4[3:1]. eci is switched from p1.2 to p4.1 pca0/pwm0 is switched from p1.3 to p4.2 pca1/pwm1 is switched from p1.4 to p4.3 spi_p4 0 : default. the spi function is on p1[7:4] 1 : the spi function on p1[7:4] is switched to p4[3:0]. sclk is switched from p1.7 to p4.3 mosi is switched from p1.6 to p4.2 miso is switched from p1.5 to p4.1 ss is switched from p1.4 to p4.0 s2_p4 0 : default. the uart2(s2) function is on p1[3:2] 1 : the uart2(s2) function on p1[3:2] is switched to p4[3:2]. txd2 is switched from p1.3 to p4.3 rxd2 is switched from p1.2 to p4.2 gf2 : general flag. it can be used by software. adrj 0 : the 10-bit conversion result of adc is arranged as {adc_res[7:0], adc_resl[1:0]}. 1 : the 10-bit conversion result is right-justified, {adc_res[1:0], adc_resl[7:0]}. dps 0 : default. dptr0 is selected as data pointer. 1 : the secondary dptr is switched to use. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 324 www.stcmcu.com 5. registers related with spi interrupt : ie, ie2, ip2 and ip2h ie: interrupt enable rsgister (bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ie a8h name ea elvd eadc es et1 ex1 et0 ex0 ea : disables all interrupts. if ea = 0,no interrupt will be acknowledged. if ea = 1, each interrupt source is individually enabled or disabled by setting or clearing its enable bit. ie2: interrupt enable 2 rsgister (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ie2 afh name - - - - - - espi es2 espi : spi interrupt enable bit. if espi = 0, spi interrupt will be diabled. if espi = 1, spi interrupt is enabled. ip2h: interrupt priority high register (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ip2h b6h name - - - - - - pspih ps2h ip2: interrupt priority register (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 ip2 b5h name - - - - - - pspi ps2 pspih, pspi: spi interrupt priority control bits. if pspih=0 and pspi=0, spi interrupt is assigned lowest priority (priority 0). if pspih=0 and pspi=1, spi interrupt is assigned lower priority (priority 1). if pspih=1 and pspi=0, spi interrupt is assigned higher priority (priority 2). if pspih=1 and pspi=1, spi interrupt is assigned highest priority (priority 3). ps2h, ps2 : serial port 2 (uart2) interrupt priority control bits. if ps2h=0 and ps2=0, uart2 interrupt is assigned lowest priority (priority 0). if ps2h=0 and ps2=1, uart2 interrupt is assigned lower priority (priority 1). if ps2h=1 and ps2=0, uart2 interrupt is assigned higher priority (priority 2). if ps2h=1 and ps2=1, uart2 interrupt is assigned highest priority (priority 3). stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 325 www.stcmcu.com cpu clock clock divider 4, 16, 64, 128 selection spr1 spr0 spi control spi state register: spstat spi interrupt request 8-bit shift register read data buffer clock logic ssig spi control register: spctl s s m m s m i/o control miso p1.6 mosi p1.5 sclk p1.7 ss p1.4 mstr spen spen dord mstr cpha cpol spr1 spr0 internal data bus clock spi clock(host) spif wcol spi block diagram mstr spen the spi interface has three pins implementing the spi functionality: sclk(p1.7), miso(p1.6), mosi(p1.5). an extra pin ss(p1.4) is designed to configure the spi to run under master or slave mode. sclk, mosi and miso are typically tied together between two or more spi devices. data flows from master to slave on mosi(master out slave in) pin and flows from slave to master on miso(master in slave out) pin. the sclk signal is output in the master mode and is input in the slave mode. if the spi system is disabled, i.e, spen(spctl.6)=0, these pins are configured as general-purposed i/o port(p1.4 ~ p1.7). ss is thel slave select pin. in a typical configuration, an spi master asserts one of its port pins to select one spi device as the current slave. an spi slave device uses its ss pin to determine whether it is selected. but if spen=0 or ssig(spctl.7) bit is 1, the ss pin is ignored. note that even if the spi is configured as a master(mstr/ spctl.4=1), it can still be converted to a slave by driving the ss pin low. when the conversion happened, the spif bit(spstat.7) will be set. 11.2 spi structure two devices with spi interface communicate with each other via one synchronous clock signal, one input data signal, and one output data signal. there are two concerns the user should take care, one of them is latching data on the negative edge or positive edge of the clock signal which named polarity, the other is keeping the clock signal low or high while the device idle which named phase. permuting those states from polarity and phase, there could be four modes formed, they are spi-mode-0, spi-mode-1, spi-mode-2, spi-mode-3. many device declares that they meet spi machanism, but few of them are adaptive to all four modes. the stc12c5a60s2 series are flexible to be configured to communicate to another device with mode-0, mode-1, mode-2 or mode-3 spi, and play part of master and slave. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 326 www.stcmcu.com table: spi master and slave selection spen ssig ss (p1.4) mstr mode miso (p1.6) mosi (p1.5) spiclk (p1.7) remark 0 x p1.4 x spi disable gpi/o p1.6 gpi/o p1.5 gpi/o p1.7 spi is disabled. p1.4/p1.5/p1.6/ p1.7 as gpio. 1 0 0 0 selected salve output input input selected as slave 1 0 1 0 unselected slave hi-z input input not selected. 1 0 0 1->0 slave output input input convert from master to slave 1 0 1 1 master (idle) input high- impedance high- impedance mosi and sclk are in high- impedance state in order to avoid bus clash when master is idle. whether is sclk pulled up or pulled down depends on cpol/spctl.3. but it do not be allowed that sclk is suspended. master (active) output output mosi and sclk is strong push- pull output. 1 1 p1.4 0 slave output input input slave 1 1 p1.4 1 master input output output master 11.3 spi data communication 11.3.1 spi configuration when spi data communication, spen, ssig, ss(p1.4) and mstr jointly control the selection of master and slave. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 327 www.stcmcu.com 8-bit shift register spi clock generator 8-bit shift register master slave miso mosi spiclk port spiclk mosi /ss spi single master single slave configuration miso 8-bit shift register master/slave miso mosi spiclk spiclk mosi /ss spi dual device configuration, both can be a master or slave /ss miso spi clock generator spi clock generator 8-bit shift register master/slave miso mosi spiclk spiclk mosi /ss spi single master multiple slaves configuration port miso miso spiclk mosi /ss port 8-bit shift register spi clock generator 8-bit shift register 8-bit shift register slave #1 slave #1 master there are 3 spi data communication modes: single master single slave, dual devices configuration (both can be a master or slave) and single master multiple slaves. 11.3.2 spi data communication modes stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 328 www.stcmcu.com in spi, transfers are always initiated by the master. if the spi is enabled(spen=1) and selected as master, any instruction that use spi data register spdat as the destination will starts the spi clock generator and a data transfer. the data will start to appear on mosi about one half spi bit-time to one spi bit-time after it. before starting the transfer, the master may select a slave by driving the ss pin of the corresponding device low. data written to the spdat register of the master shifted out of mosi pin of the master to the mosi pin of the slave. and at the same time the data in spdat register of the selected slave is shifted out of miso pin to the miso pin of the master. during one byte transfer, data in the master and in the slave is interchanged. after shifting one byte, the transfer completion flag(spif) is set and an interrupt will be created if the spi interrupt is enabled. if spen=1, ssig=0, ss pin=1 and mstr=1, the spi is enabled in master mode. before the instruction that use spdat as the destination register, the master is in idle state and can be selected as slave device by any other master drives the idle master ss pin low. once this happened, mstr bit of the idle master is cleared by hardware and changes its state a selected slave. user software should always check the mstr bit. if this bit is cleared by the mode change of ss pin and the user wants to continue to use the spi as a master later, the user must set the mstr bit again, otherwise it will always stay in slave mode. the spi is single buffered in transmit direction and double buffered in receive direction. new data for transmission can not be written to the shift register until the previous transaction is complete. the wcol bit is set to signal data collision when the data register is written during transaction. in this case, the data currently being transmitted will continue to be transmitted, but the new data which causing the collision will be lost. for receiving data, received data is transferred into a internal parallel read data buffer so that the shift register is free to accept a second byte. however, the received byte must be read from the data register(spdat) before the next byte has been completely transferred. otherwise the previous byte is lost. wcol can be cleared in software by writing 1 to the bit. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 329 www.stcmcu.com clock cycle sclk(cpol=0) mosi(input) 1 2 3 4 5 6 7 8 sclk(cpol=1) dord=0 dord=1 msb lsb 6 1 5 2 4 3 3 4 1 6 2 5 lsb msb msb lsb 6 1 5 2 4 3 3 4 2 5 1 6 lsb msb dord=0 dord=1 miso(output) ss pin(if ssig bit=0) spi slave transfer format with cpha=1 1) 1)undefined 11.3.3 spi data modes cpha/spctl.2 is spi clock phase select bit which is used to setting the clock edge of data sample and change. cpol/spctl.3 is used to select spi clock polarity. the following are some typical timing diagrams which depend on the value of cpha/spctl.2 clock cycle sclk(cpol=0) mosi(input) 1 2 3 4 5 6 7 8 sclk(cpol=1) dord=0 dord=1 msb lsb 6 1 5 2 4 3 3 4 1 6 2 5 lsb msb msb lsb 6 1 5 2 4 3 3 4 2 5 1 6 lsb msb dord=0 dord=1 miso(output) ss pin(if ssig bit=0) undefined spi slave transfer format with cpha=0 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 330 www.stcmcu.com clock cycle sclk(cpol=0) mosi(input) 1 2 3 4 5 6 7 8 sclk(cpol=1) dord=0 dord=1 msb lsb 6 1 5 2 4 3 3 4 1 6 2 5 lsb msb msb lsb 6 1 5 2 4 3 3 4 2 5 1 6 lsb msb dord=0 dord=1 miso(output) ss pin(if ssig bit=0) spi master transfer format with cpha=0 clock cycle sclk(cpol=0) mosi(input) 1 2 3 4 5 6 7 8 sclk(cpol=1) msb lsb 6 1 5 2 4 3 3 4 1 6 2 5 lsb msb miso(output) spi master transfer format with cpha=1 dord=0 dord=1 msb lsb 6 1 5 2 4 3 3 4 1 6 2 5 lsb msb dord=0 dord=1 ss pin(if ssig bit=0) *when p4spi bit in auxr1 register is set, the function of spi is redirected from p3[7:4] to p4[7:4] pin by pin. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 331 www.stcmcu.com 11.4 spi function demo programs (single master single slave) 11.4.1 spi function demo programs using interrupts (c and asm) the following program,written in c language and assembly language, tests spi function and applys to spi single master single slave configuration. 1. c code listing: ; /*----------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited -------------------------------------*/ ;/* --- stc 1t series mcu spi demo (1 master and 1 slave) -----------*/ ;/* --- mobile: (86)13922809991 ---------------------------------------------*/ ;/* --- fax: 86-755-82905966 -------------------------------------------------*/ ;/* --- tel: 86-755-82948412 --------------------------------------------------*/ ;/* --- web: www.stcmcu.com ---------------------------------------------*/ ;/* if you want to use the program or the program referenced in the ----*/ ;/* article, please specify in which data and procedures from stc ----*/ ;/*-----------------------------------------------------------------------------------*/ #include "reg51.h" #define master //define:master undefine:slave #define fosc 18432000l #define baud (256 - fosc / 32 / 115200) typedef unsigned char byte; typedef unsigned int word; typedef unsigned long dword; sfr auxr = 0x8e; //auxiliary register sfr spstat = 0xcd; //spi status register #define spif 0x80 //spstat.7 #define wcol 0x40 //spstat.6 sfr spctl = 0xce; //spi control register #define ssig 0x80 //spctl.7 #define spen 0x40 //spctl.6 #define dord 0x20 //spctl.5 #define mstr 0x10 //spctl.4 #define cpol 0x08 //spctl.3 #define cpha 0x04 //spctl.2 #define spdhh 0x00 //cpu_clk/4 #define spdh 0x01 //cpu_clk/16 #define spdl 0x02 //cpu_clk/64 #define spdll 0x03 //cpu_clk/128 sfr spdat = 0xcf; //spi data register sbit spiss = p1^3; //spi slave select, connect to slave' ss(p1.4) pin stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 332 www.stcmcu.com sfr ie2 = 0xaf; //interrupt enable rgister 2 #define espi 0x02 //ie2.1 void inituart(); void initspi(); void senduart(byte dat); //send data to pc byte recvuart(); //receive data from pc /////////////////////////////////////////////////////////// void main() { inituart(); //initial uart initspi(); //initial spi ie2 |= espi; ea = 1; while (1) { #ifdef master //for master (receive uart data from pc and send it to slave, //in the meantime receive spi data from slave and send it to pc) acc = recvuart(); spiss = 0; //pull low slave ss spdat = acc; //trigger spi send #endif } } /////////////////////////////////////////////////////////// void spi_isr( ) interrupt 9 using 1 //spi interrupt routine 9 (004bh) { spstat = spif | wcol; //clear spi status #ifdef master spiss = 1; //push high slave ss senduart(spdat); //return received spi data #else //for salve (receive spi data from master and spdat = spdat; // send previous spi data to master) #endif } /////////////////////////////////////////////////////////// stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 333 www.stcmcu.com void inituart() { scon = 0x5a; //set uart mode as 8-bit variable baudrate tmod = 0x20; //timer1 as 8-bit auto reload mode auxr = 0x40; //timer1 work at 1t mode th1 = tl1 = baud; //115200 bps tr1 = 1; } /////////////////////////////////////////////////////////// void initspi() { spdat = 0; //initial spi data spstat = spif | wcol; //clear spi status #ifdef master spctl = spen | mstr; //master mode #else spctl = spen; //slave mode #endif } /////////////////////////////////////////////////////////// void senduart(byte dat) { while (!ti); //wait pre-data sent ti = 0; //clear ti flag sbuf = dat; //send current data } /////////////////////////////////////////////////////////// byte recvuart() { while (!ri); //wait receive complete ri = 0; //clear ri flag return sbuf; //return receive data } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 334 www.stcmcu.com 2. assemly code listing: ; /*----------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited -------------------------------------*/ ;/* --- stc 1t series mcu spi demo (1 master and 1 slave) -----------*/ ;/* --- mobile: (86)13922809991 ---------------------------------------------*/ ;/* --- fax: 86-755-82905966 -------------------------------------------------*/ ;/* --- tel: 86-755-82948412 --------------------------------------------------*/ ;/* --- web: www.stcmcu.com ---------------------------------------------*/ ;/* if you want to use the program or the program referenced in the ----*/ ;/* article, please specify in which data and procedures from stc ----*/ ;/*-----------------------------------------------------------------------------------*/ //#define master //define:master undefine:slave auxr data 08eh ;auxiliary register spstat data 0cdh ;spi status register spif equ 080h ;spstat.7 wcol equ 040h ;spstat.6 spctl data 0ceh ;spi control register ssig equ 080h ;spctl.7 spen equ 040h ;spctl.6 dord equ 020h ;spctl.5 mstr equ 010h ;spctl.4 cpol equ 008h ;spctl.3 cpha equ 004h ;spctl.2 spdhh equ 000h ;cpu_clk/4 spdh equ 001h ;cpu_clk/16 spdl equ 002h ;cpu_clk/64 spdll equ 003h ;cpu_clk/128 spdat data 0cfh ;spi data register spiss bit p1.3 ;spi slave select, connect to slave' ss(p1.4) pin ie2 equ 0afh ;interrupt enable rgister 2 espi equ 02h ;ie2.1 ;////////////////////////////////////////////////////////// org 0000h ljmp reset org 004bh ;spi interrupt routine spi_isr: push acc push psw mov spstat, #spif | wcol ;clear spi status stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 335 www.stcmcu.com #ifdef master setb spiss ;push high slave ss mov a, spdat ;return received spi data lcall send_uart #else //for salve (receive spi data from master and mov spdat, spdat ; send previous spi data to master) #endif pop psw pop acc reti ;////////////////////////////////////////////////////////// org 0100h reset: lcall init_uart ;initial uart lcall init_spi ;initial spi orl ie2, #espi setb ea main: #ifdef master //for master (receive uart data from pc and send it to slave, lcall recv_uart ; in the meantimereceive spi data from slave and send it to pc) clr spiss ;pull low slave ss mov spdat, a ;trigger spi send #endif sjmp main ;////////////////////////////////////////////////////////// init_uart: mov scon, #5ah ;set uart mode as 8-bit variable baudrate mov tmod, #20h ;timer1 as 8-bit auto reload mode mov auxr, #40h ;timer1 work at 1t mode mov tl1, #0fbh ;115200 bps(256 - 18432000 / 32 / 115200) mov th1, #0fbh setb tr1 ret ;////////////////////////////////////////////////////////// stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 336 www.stcmcu.com init_spi: mov spdat, #0 ;initial spi data mov spstat, #spif | wcol ;clear spi status #ifdef master mov spctl, #spen | mstr ;master mode #else mov spctl, #spen ;slave mode #endif ret ;////////////////////////////////////////////////////////// send_uart: jnb ti, $ ;wait pre-data sent clr ti ;clear ti flag mov sbuf, a ;send current data ret ;////////////////////////////////////////////////////////// recv_uart: jnb ri,$ ;wait receive complete clr ri ;clear ri flag mov a, sbuf ;return receive data ret ret ;////////////////////////////////////////////////////////// end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 337 www.stcmcu.com 11.4.2 spi function demo programs using polling (c and asm) 1. c code listing: ; /*-----------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited --------------------------------------*/ ;/* --- stc 1t series mcu spi demo (1 master and 1 slave) ------------*/ ;/* --- mobile: (86)13922809991 ----------------------------------------------*/ ;/* --- fax: 86-755-82905966 --------------------------------------------------*/ ;/* --- tel: 86-755-82948412 ---------------------------------------------------*/ ;/* --- web: www.stcmcu.com ---------------------------------------------*/ ;/* if you want to use the program or the program referenced in the ----*/ ;/* article, please specify in which data and procedures from stc ----*/ ;/*----------------------------------------------------------------------------------*/ #include "reg51.h" //#define master //define:master undefine:slave #define fosc 18432000l #define baud (256 - fosc / 32 / 115200) typedef unsigned char byte; typedef unsigned int word; typedef unsigned long dword; sfr auxr = 0x8e; //auxiliary register sfr spstat = 0xcd; //spi status register #define spif 0x80 //spstat.7 #define wcol 0x40 //spstat.6 sfr spctl = 0xce; //spi control register #define ssig 0x80 //spctl.7 #define spen 0x40 //spctl.6 #define dord 0x20 //spctl.5 #define mstr 0x10 //spctl.4 #define cpol 0x08 //spctl.3 #define cpha 0x04 //spctl.2 #define spdhh 0x00 //cpu_clk/4 #define spdh 0x01 //cpu_clk/16 #define spdl 0x02 //cpu_clk/64 #define spdll 0x03 //cpu_clk/128 sfr spdat = 0xcf; //spi data register sbit spiss = p1^3; //spi slave select, connect to slave' ss(p1.4) pin void inituart(); void initspi(); void senduart(byte dat); //send data to pc byte recvuart(); //receive data from pc byte spiswap(byte dat); //swap spi data between master stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 338 www.stcmcu.com /////////////////////////////////////////////////////////// void main() { inituart(); //initial uart initspi(); //initial spi while (1) { #ifdef master //for master (receive uart data from pc and send it to slave, // in the meantime receive spi data from slave and send it to pc) senduart(spiswap(recvuart())); #else //for salve (receive spi data from master and acc = spiswap(acc); // send previous spi data to master) #endif } } /////////////////////////////////////////////////////////// void inituart() { scon = 0x5a; //set uart mode as 8-bit variable baudrate tmod = 0x20; //timer1 as 8-bit auto reload mode auxr = 0x40; //timer1 work at 1t mode th1 = tl1 = baud; //115200 bps tr1 = 1; } /////////////////////////////////////////////////////////// void initspi() { spdat = 0; //initial spi data spstat = spif | wcol; //clear spi status #ifdef master spctl = spen | mstr; //master mode #else spctl = spen; //slave mode #endif } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 339 www.stcmcu.com /////////////////////////////////////////////////////////// void senduart(byte dat) { while (!ti); //wait pre-data sent ti = 0; //clear ti flag sbuf = dat; //send current data } /////////////////////////////////////////////////////////// byte recvuart() { while (!ri); //wait receive complete ri = 0; //clear ri flag return sbuf; //return receive data } /////////////////////////////////////////////////////////// byte spiswap(byte dat) { #ifdef master spiss = 0; //pull low slave ss #endif spdat = dat; //trigger spi send while (!(spstat & spif)); //wait send complete spstat = spif | wcol; //clear spi status #ifdef master spiss = 1; //push high slave ss #endif return spdat; //return received spi data } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 340 www.stcmcu.com 2. assemly code listing: ; /*----------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited -------------------------------------*/ ;/* --- stc 1t series mcu spi demo (1 master and 1 slave) -----------*/ ;/* --- mobile: (86)13922809991 ---------------------------------------------*/ ;/* --- fax: 86-755-82905966 -------------------------------------------------*/ ;/* --- tel: 86-755-82948412 --------------------------------------------------*/ ;/* --- web: www.stcmcu.com ---------------------------------------------*/ ;/* if you want to use the program or the program referenced in the ----*/ ;/* article, please specify in which data and procedures from stc ----*/ ;/*-----------------------------------------------------------------------------------*/ //#define master //define:master undefine:slave auxr data 08eh ;auxiliary register spstat data 0cdh ;spi status register spif equ 080h ;spstat.7 wcol equ 040h ;spstat.6 spctl data 0ceh ;spi control register ssig equ 080h ;spctl.7 spen equ 040h ;spctl.6 dord equ 020h ;spctl.5 mstr equ 010h ;spctl.4 cpol equ 008h ;spctl.3 cpha equ 004h ;spctl.2 spdhh equ 000h ;cpu_clk/4 spdh equ 001h ;cpu_clk/16 spdl equ 002h ;cpu_clk/64 spdll equ 003h ;cpu_clk/128 spdat data 0cfh ;spi data register spiss bit p1.3 ;spi slave select, connect to slave' ss(p1.4) pin ;////////////////////////////////////////////////////////// org 0000h ljmp reset org 0100h reset: lcall init_uart ;initial uart lcall init_spi ;initial spi stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 341 www.stcmcu.com main: #ifdef maste //for master (receive uart data from pc and send it to slave, in the meantime lcall recv_uart ; receive spi data from slave and send it to pc) lcall spi_swap lcall send_uart #else //for salve (receive spi data from master and lcall spi_swap ; send previous spi data to master) #endif sjmp main ;////////////////////////////////////////////////////////// init_uart: mov scon, #5ah ;set uart mode as 8-bit variable baudrate mov tmod, #20h ;timer1 as 8-bit auto reload mode mov auxr, #40h ;timer1 work at 1t mode mov tl1, #0fbh ;115200 bps(256 - 18432000 / 32 / 115200) mov th1, #0fbh setb tr1 ret ;////////////////////////////////////////////////////////// init_spi: mov spdat, #0 ;initial spi data mov spstat, #spif | wcol ;clear spi status #ifdef master mov spctl, #spen | mstr ;master mode #else mov spctl, #spen ;slave mode #endif ret ;////////////////////////////////////////////////////////// send_uart: jnb ti, $ ;wait pre-data sent clr ti ;clear ti flag mov sbuf, a ;send current data ret ;////////////////////////////////////////////////////////// stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 342 www.stcmcu.com recv_uart: jnb ri, $ ;wait receive complete clr ri ;clear ri flag mov a, sbuf ;return receive data ret ret ;////////////////////////////////////////////////////////// spi_swap: #ifdef master clr spiss ;pull low slave ss #endif mov spdat, a ;trigger spi send wait: mov a, spstat jnb acc.7, wait ;wait send complete mov spstat, #spif | wcol ;clear spi status #ifdef master setb spiss ;push high slave ss #endif mov a, spdat ;return received spi data ret ;////////////////////////////////////////////////////////// end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 343 www.stcmcu.com 11.5 spi function demo programs (each other as the master-slave) 11.5.1 spi function demo programs using interrupts (c and asm) 1. c code listing: /*--------------------------------------------------------------------------------------------*/ /* --- stc mcu international limited -----------------------------------------------*/ /* --- stc12c5axx series mcu spi demo(each other as the master-slave) --*/ /* --- mobile: (86)13922809991 -------------------------------------------------------*/ /* --- fax: 86-755-82905966 -----------------------------------------------------------*/ /* --- tel: 86-755-82948412 ------------------------------------------------------------*/ /* --- web: www.stcmcu.com -------------------------------------------------------*/ /* if you want to use the program or the program referenced in the --------------*/ /* article, please specify in which data and procedures from stc --------------*/ /*---------------------------------------------------------------------------------------------*/ #include "reg51.h" #define fosc 18432000l #define baud (256 - fosc / 32 / 115200) typedef unsigned char byte; typedef unsigned int word; typedef unsigned long dword; sfr auxr = 0x8e; //auxiliary register sfr spstat = 0xcd; //spi status register #define spif 0x80 //spstat.7 #define wcol 0x40 //spstat.6 sfr spctl = 0xce; //spi control register #define ssig 0x80 //spctl.7 #define spen 0x40 //spctl.6 #define dord 0x20 //spctl.5 #define mstr 0x10 //spctl.4 #define cpol 0x08 //spctl.3 #define cpha 0x04 //spctl.2 #define spdhh 0x00 //cpu_clk/4 #define spdh 0x01 //cpu_clk/16 #define spdl 0x02 //cpu_clk/64 #define spdll 0x03 //cpu_clk/128 sfr spdat = 0xcf; //spi data register sbit spiss = p1^3; //spi slave select, connect to other mcu's ss(p1.4) pin sfr ie2 = 0xaf; //interrupt enable rgister 2 #define espi 0x02 //ie2.1 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 344 www.stcmcu.com void inituart(); void initspi(); void senduart(byte dat); //send data to pc byte recvuart(); //receive data from pc bit mssel; //1: master 0:slave /////////////////////////////////////////////////////////// void main() { inituart(); //initial uart initspi(); //initial spi ie2 |= espi; ea = 1; while (1) { if (ri) { spctl = spen | mstr; //set as master mssel = 1; acc = recvuart(); spiss = 0; //pull low slave ss spdat = acc; //trigger spi send } } } /////////////////////////////////////////////////////////// void spi_isr() interrupt 9 using 1 //spi interrupt routine 9 (004bh) { spstat = spif | wcol; //clear spi status if (mssel) { spctl = spen; //reset as slave mssel = 0; spiss = 1; //push high slave ss senduart(spdat); //return received spi data } else { //for salve (receive spi data from master and spdat = spdat; // send previous spi data to master) } } /////////////////////////////////////////////////////////// stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 345 www.stcmcu.com void inituart() { scon = 0x5a; //set uart mode as 8-bit variable baudrate tmod = 0x20; //timer1 as 8-bit auto reload mode auxr = 0x40; //timer1 work at 1t mode th1 = tl1 = baud; //115200 bps tr1 = 1; } /////////////////////////////////////////////////////////// void initspi() { spdat = 0; //initial spi data spstat = spif | wcol; //clear spi status spctl = spen; //slave mode } /////////////////////////////////////////////////////////// void senduart(byte dat) { while (!ti); //wait pre-data sent ti = 0; //clear ti flag sbuf = dat; //send current data } /////////////////////////////////////////////////////////// byte recvuart() { while (!ri); //wait receive complete ri = 0; //clear ri flag return sbuf; //return receive data } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 346 www.stcmcu.com 2 . assembly code listing: /*--------------------------------------------------------------------------------------------*/ /* --- stc mcu international limited -----------------------------------------------*/ /* --- stc12c5axx series mcu spi demo(each other as the master-slave) --*/ /* --- mobile: (86)13922809991 -------------------------------------------------------*/ /* --- fax: 86-755-82905966 -----------------------------------------------------------*/ /* --- tel: 86-755-82948412 ------------------------------------------------------------*/ /* --- web: www.stcmcu.com -------------------------------------------------------*/ /* if you want to use the program or the program referenced in the --------------*/ /* article, please specify in which data and procedures from stc --------------*/ /*---------------------------------------------------------------------------------------------*/ auxr data 08eh ;auxiliary register spstat data 0cdh ;spi status register spif equ 080h ;spstat.7 wcol equ 040h ;spstat.6 spctl data 0ceh ;spi control register ssig equ 080h ;spctl.7 spen equ 040h ;spctl.6 dord equ 020h ;spctl.5 mstr equ 010h ;spctl.4 cpol equ 008h ;spctl.3 cpha equ 004h ;spctl.2 spdhh equ 000h ;cpu_clk/4 spdh equ 001h ;cpu_clk/16 spdl equ 002h ;cpu_clk/64 spdll equ 003h ;cpu_clk/128 spdat data 0cfh ;spi data register spiss bit p1.3 ;spi slave select, connect to other mcu's ss(p1.4) pin ie2 equ 0afh ;interrupt enable rgister 2 espi equ 02h ;ie2.1 mssel bit 20h.0 ;1: master 0:slave ;////////////////////////////////////////////////////////// org 0000h ljmp reset org 004bh ;spi interrupt routine spi_isr: push acc push psw mov spstat, #spif | wcol ;clear spi status jbc mssel, master_send stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 347 www.stcmcu.com slave_recv: //for salve (receive spi data from master and mov spdat, spdat ; send previous spi data to master) jmp spi_exit master_send: setb spiss ;push high slave ss mov spctl, #spen ; ;reset as slave mov a, spdat ;return received spi data lcall send_uart spi_exit: pop psw pop acc reti ;////////////////////////////////////////////////////////// org 0100h reset: mov sp,#3fh lcall init_uart ;initial uart lcall init_spi ;initial spi orl ie2,#espi setb ea main: jnb ri, $ ;wait uart data mov spctl, #spen | mstr ; ;set as master setb mssel lcall recv_uart ;receive uart data from pc clr spiss ;pull low slave ss mov spdat,a ;trigger spi send sjmp main ;////////////////////////////////////////////////////////// init_uart: mov scon, #5ah ;set uart mode as 8-bit variable baudrate mov tmod, #20h ;timer1 as 8-bit auto reload mode mov auxr ,#40h ;timer1 work at 1t mode mov tl1, #0fbh ;115200 bps(256 - 18432000 / 32 / 115200) mov th1, #0fbh setb tr1 ret stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 348 www.stcmcu.com ;////////////////////////////////////////////////////////// init_spi: mov spdat, #0 ;initial spi data mov spstat, #spif | wcol ;clear spi status mov spctl, #spen ;slave mode ret ;////////////////////////////////////////////////////////// send_uart: jnb ti, $ ;wait pre-data sent clr ti ;clear ti flag mov sbuf, a ;send current data ret ;////////////////////////////////////////////////////////// recv_uart: jnb ri, $ ;wait receive complete clr ri ;clear ri flag mov a, sbuf ;return receive data ret ret ;////////////////////////////////////////////////////////// end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 349 www.stcmcu.com 11.5.2 spi function demo programs using polling 1. c code listing: /*--------------------------------------------------------------------------------------------*/ /* --- stc mcu international limited -----------------------------------------------*/ /* --- stc12c5axx series mcu spi demo(each other as the master-slave) --*/ /* --- mobile: (86)13922809991 -------------------------------------------------------*/ /* --- fax: 86-755-82905966 -----------------------------------------------------------*/ /* --- tel: 86-755-82948412 ------------------------------------------------------------*/ /* --- web: www.stcmcu.com -------------------------------------------------------*/ /* if you want to use the program or the program referenced in the --------------*/ /* article, please specify in which data and procedures from stc --------------*/ /*---------------------------------------------------------------------------------------------*/ #include "reg51.h" #define fosc 18432000l #define baud (256 - fosc / 32 / 115200) typedef unsigned char byte; typedef unsigned int word; typedef unsigned long dword; sfr auxr = 0x8e; //auxiliary register sfr spstat = 0xcd; //spi status register #define spif 0x80 //spstat.7 #define wcol 0x40 //spstat.6 sfr spctl = 0xce; //spi control register #define ssig 0x80 //spctl.7 #define spen 0x40 //spctl.6 #define dord 0x20 //spctl.5 #define mstr 0x10 //spctl.4 #define cpol 0x08 //spctl.3 #define cpha 0x04 //spctl.2 #define spdhh 0x00 //cpu_clk/4 #define spdh 0x01 //cpu_clk/16 #define spdl 0x02 //cpu_clk/64 #define spdll 0x03 //cpu_clk/128 sfr spdat = 0xcf; //spi data register sbit spiss = p1^3; //spi slave select, connect to slave' ss(p1.4) pin void inituart(); void initspi(); void senduart(byte dat); //send data to pc byte recvuart(); //receive data from pc byte spiswap(byte dat); //swap spi data between master stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 350 www.stcmcu.com /////////////////////////////////////////////////////////// void main() { inituart(); //initial uart initspi(); //initial spi while (1) { if (ri) { spctl = spen | mstr; //set as master senduart(spiswap(recvuart())); spctl = spen; //reset as slave } if (spstat & spif) { spstat = spif | wcol; //clear spi status spdat = spdat; //mov data from receive buffer to send buffer } } } /////////////////////////////////////////////////////////// void inituart() { scon = 0x5a; //set uart mode as 8-bit variable baudrate tmod = 0x20; //timer1 as 8-bit auto reload mode auxr = 0x40; //timer1 work at 1t mode th1 = tl1 = baud; //115200 bps tr1 = 1; } /////////////////////////////////////////////////////////// void initspi() { spdat = 0; //initial spi data spstat = spif | wcol; //clear spi status spctl = spen; //slave mode } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 351 www.stcmcu.com /////////////////////////////////////////////////////////// void senduart(byte dat) { while (!ti); //wait pre-data sent ti = 0; //clear ti flag sbuf = dat; //send current data } /////////////////////////////////////////////////////////// byte recvuart() { while (!ri); //wait receive complete ri = 0; //clear ri flag return sbuf; //return receive data } /////////////////////////////////////////////////////////// byte spiswap(byte dat) { spiss = 0; //pull low slave ss spdat = dat; //trigger spi send while (!(spstat & spif)); //wait send complete spstat = spif | wcol; //clear spi status spiss = 1; //push high slave ss return spdat; //return received spi data } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 352 www.stcmcu.com 2. assemly code listing: /*--------------------------------------------------------------------------------------------*/ /* --- stc mcu international limited -----------------------------------------------*/ /* --- stc12c5axx series mcu spi demo(each other as the master-slave) --*/ /* --- mobile: (86)13922809991 -------------------------------------------------------*/ /* --- fax: 86-755-82905966 -----------------------------------------------------------*/ /* --- tel: 86-755-82948412 ------------------------------------------------------------*/ /* --- web: www.stcmcu.com -------------------------------------------------------*/ /* if you want to use the program or the program referenced in the --------------*/ /* article, please specify in which data and procedures from stc --------------*/ /*---------------------------------------------------------------------------------------------*/ auxr data 08eh ;auxiliary register spstat data 0cdh ;spi status register spif equ 080h ;spstat.7 wcol equ 040h ;spstat.6 spctl data 0ceh ;spi control register ssig equ 080h ;spctl.7 spen equ 040h ;spctl.6 dord equ 020h ;spctl.5 mstr equ 010h ;spctl.4 cpol equ 008h ;spctl.3 cpha equ 004h ;spctl.2 spdhh equ 000h ;cpu_clk/4 spdh equ 001h ;cpu_clk/16 spdl equ 002h ;cpu_clk/64 spdll equ 003h ;cpu_clk/128 spdat data 0cfh ;spi data register spiss bit p1.3 ;spi slave select, connect to slave' ss(p1.4) pin ;////////////////////////////////////////////////////////// org 0000h ljmp reset org 0100h reset: lcall init_uart ;initial uart lcall init_spi ;initial spi main: jb ri, master_mode stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 353 www.stcmcu.com slave_mode: mov a, spstat jnb acc.7, main mov spstat, #spif | wcol ;clear spi status mov spdat, spdat ;return received spi data sjmp main master_mode: mov spctl, #spen | mstr ;set as master lcall recv_uart ;receive uart data from pc lcall spi_swap ;send it to slave, in the meantime, receive spi data from slave lcall send_uart ;send spi data to pc mov spctl, #spen ; ;reset as slave sjmp main ;////////////////////////////////////////////////////////// init_uart: mov scon, #5ah ;set uart mode as 8-bit variable baudrate mov tmod, #20h ;timer1 as 8-bit auto reload mode mov auxr, #40h ;timer1 work at 1t mode mov tl1, #0fbh ;115200 bps(256 - 18432000 / 32 / 115200) mov th1, #0fbh setb tr1 ret ;////////////////////////////////////////////////////////// init_spi: mov spdat, #0 ;initial spi data mov spstat, #spif | wcol ;clear spi status mov spctl, #spen ;slave mode ret ;////////////////////////////////////////////////////////// send_uart: jnb ti, $ ;wait pre-data sent clr ti ;clear ti flag mov sbuf, a ;send current data ret stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 354 www.stcmcu.com ;////////////////////////////////////////////////////////// recv_uart: jnb ri, $ ;wait receive complete clr ri ;clear ri flag mov a, sbuf ;return receive data ret ret ;////////////////////////////////////////////////////////// spi_swap: clr spiss ;pull low slave ss mov spdat, a ;trigger spi send wait: mov a, spstat jnb acc.7, wait ;wait send complete mov spstat, #spif | wcol ;clear spi status setb spiss ;push high slave ss mov a, spdat ;return received spi data ret ;////////////////////////////////////////////////////////// end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 355 www.stcmcu.com 11.6 spi demo (single master multiple slave) 1. assemly code listing ;/*---------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited ------------------------------------*/ ;/* --- stc 1t series mcu spi asm demo ------------------------------*/ ;/* --- mobile: (86)13922809991 --------------------------------------------*/ ;/* --- fax: 86-755-82905966 ------------------------------------------------*/ ;/* --- tel: 86-755-82948412 ------------------------------------------------*/ ;/* --- web: www.stcmcu.com -------------------------------------------*/ ;/* if you want to use the program or the program referenced in the */ ;/* article, please specify in which data and procedures from stc */ ;/*--------------------------------------------------------------------------------*/ ;1. the demo program is suitable for single master multiple slave system ;2. hardware connection: ; mosi sclk p1.2 miso ss slave #1 master slave #2 p1.3 miso mosi sclk miso mosi sclk ss ;3. spi communication : 8-bit master mcu spi register and 8-bit slave mcu spi register combined into a 16-bit cyclic shift register. when master mcu is written a byte data to spi data register (spdat), the data transmission is triggered immediately. with the sclks clock signal, 8-bit data in master mcus spdat register shift into slave mcu s spdat through mosi pin, in the meanwhile, the 8-bit data in slave mcus spdat register is shifted into master mcus spdat register through miso pin. ;4. modification method : a) set master_slave equ 0, then the object file is master mcu file. b) set master_slave equ 1, then the object file is slave #1 mcu file. c) set master_slave equ 2, then the object file is slave #2 mcu file. d) power-on the whole system (master mcu, slave #1 mcu and slave #2 mcu) e) p1.2 and p1.3 respectively control slave #1 and slave #2, but still a moment, only one slave mcu is selected. f) using serial debugging assistant debug. ;5. using inquiry mothed to receive spi data ;6. work environment: fosc=18.432mhz and 9600 baudrat ; ; ; ; ; ; ; ; ; ; ; ; ; stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 356 www.stcmcu.com ;define const master_slave equ 0 ;master mcu ;master_slave equ 1 ;slave #1 mcu ;master_slave equ 2 ;slave #2 mcu ;reload_8bit_data equ 0ffh ;56700@22.1184mhz reload_8bit_data equ 0fbh ;9600@18.432mhz ;reload_8bit_data equ 0f6h ;4800@18.432mhz ;reload_8bit_data equ 0ffh ;28800@11.0592mhz ; ;define sfr auxr equ 8eh ; auxiliary register spctl equ 85h ;spi control register spstat equ 84h ;spi status register spdat equ 86h ;spi data register eadc_spi equ ie.5 ;spi interrupt enable bit ;define spi function pin sclk equ p1.7 ;spi clock pin miso equ p1.6 ;spi master input/slave output pin mosi equ p1.5 ;spi master output/slave input pin ss equ p1.4 ;spi slave select pin slave1_ss equ p1.2 ;slave #1 mcu select pin slave2_ss equ p1.3 ;slave #2 mcu select pin led_mcu_start equ p3.4 ;mcu work led ;define user variable flags equ 20h ;user flag spi_receive equ falgs.0 ;spi receive flag t0_10ms_count equ 30h ;10ms counter spi_buffer equ 31h ;spi revecie buffer ;---------------------------------------------------------------- org 0000h ljmp main org 000bh ljmp timer0_routine ;timer0 interrupt routine org 002bh ljmp adc_spi_interrupt_routine ;spi interrupt routine ;---------------------------------------------------------------- org 0080h main: clr led_mcu_start ;work led on mov sp,#7fh ;initial sp acall initial_system ;system initial if master_slave == 0 clr slave1_ss ;select slave #1 mcu stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 357 www.stcmcu.com check_rs232: jnb ri,master_check_spi ;check uart receive acall get_byte_from_rs232 ;load uart data to acc ; acall rs232_send_byte ;send data in acc to pc ; sjmp check_rs232 acall spi_send_byte ;send data in acc to spi slave sjmp check_rs232 master_check_spi: jnb spi_receive,check_rs232 ;check spi receive mov a,spi_buffer ;load spi data to acc clr spi_recevie ;clear spi receive flag acall spi_send_byte ; send data in acc to spi slave sjmp check_rs232 else slave_check_spi: jnb spi_receive,slave_check_spi ;check spi receive mov a,spi_buffer ;load spi data to acc clr spi_receive ;clear spi receive flag if master_slave == 2 add a,#1 ;value +1 on slave #2 mcu endif mov spdat,a ;save data into spdat sjmp slave_check_spi endif ;---------------------------------------------------------------- if master_slave == 0 timer0_routine: push psw push acc mov th0,#0c4h ;reload timer0 10ms value inc t0_10ms_count ;10ms counter mov a,#200 ;count 200 times clr c subb a,t0_10ms_count jnc timer0_exit cpl slave1_ss ;switch slave cpl slave2_ss mov t0_10ms_count,#0 ;reset counter timer0_exit: pop acc pop psw reti else timer0_routine: reti endif ;---------------------------------------------------------------- stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 358 www.stcmcu.com adc_spi_interrupt_routine: mov spdat,#0c0h ;clear spif and wcol flag mov a,spdat ;save spi received data mov spi_buffer,a setb spi_receive ;set spi receive flag reti ;---------------------------------------------------------------- initial_system: acall initial_uart ;initial uart sfr acall initial_spi ;initial spi sfr setb tr0 ;start timer0 setb et0 ;enable timer0 interrupt mov flags,#0 ;initial flag setb ea ;enable global interrupt flag ret ;---------------------------------------------------------------- initial_uart: mov scon,#50h ;set uart as 8-bit variable mode mov tmod,#21h ;set timer mode mov th1,#reload_8bit_data ;set uart baudrate mov tl1,#reload_8bit_data mov pcon,#80h ;baudrate * 2 orl auxr,#40h ;1t mode setb tr1 ;timer1 start ret ;---------------------------------------------------------------- initial_spi: if master_slave == 0 mov spctl,#11111100b ;master mode else mov spctl,#01101100b ;slave mode endif mov spstat,#11000000b ;clear spi flag orl auxr,#08h ;auxr.3(espi) = 1 setb eadc_spi ;enable spi interrupt ret ;---------------------------------------------------------------- rs232_send_byte: clr ti ;ready send mov sbuf,a ;write data to tx buffer jnb ti,$ ;wait send completed clr ti ;clear ti flag ret ;---------------------------------------------------------------- spi_send_byte: clr eadc_spi ;disable spi interrupt mov spdat,a ;write data to spi data register stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 359 www.stcmcu.com spi_send_byte_wait: mov a,spstat ;check spi status anl a,#80h jz spi_send_byte_wait ;wait spi send complete setb eadc_spi ;enable spi interrupt ret ;---------------------------------------------------------------- get_byte_from_rs232: mov a,sbuf ;load data to acc clr ri ;clear uart receive flag ret ;---------------------------------------------------------------- end 2. c listing code: /*---------------------------------------------------------------------------------*/ /* --- stc mcu international limited ------------------------------------*/ /* --- stc 1t series mcu spi asm demo ------------------------------*/ /* --- mobile: (86)13922809991 -------------------------------------------*/ /* --- fax: 86-755-82905966 -----------------------------------------------*/ /* --- tel: 86-755-82948412 ------------------------------------------------*/ /* --- web: www.stcmcu.com ------------------------------------------*/ /* if you want to use the program or the program referenced in the */ /* article, please specify in which data and procedures from stc */ /*-------------------------------------------------------------------------------*/ typedef unsigned char int8u; typedef unsigned int int16u; typedef unsigned long int32u; #include new_8051.h //define const #define spi_interrupt_vector 9 #define true 1 #define false 0 #define master #define config_master 0xd0 //master mode #define config_slave 0xc0 //slave mode #define spif_wcol_mask 0xc0 //spif & wcol mask bit #define fosc 1843200 #define baud 9600 #define buf_size 0x20 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 360 www.stcmcu.com //define sfr sfr spctl = 0xce; sbit led_mcu_start = p3^4; //work led bit spi_receive; //spi received flag bit spi_status; //spi status int8u spi_buffer; //spi receive data buffer int8u rs232_point; int8u isp_point; int8u buffer[buf_size]; //---------------------------------------------------------------- void initial_spi(); void init_system(); int8u get_byte_from_rs232(); void rs232_send_byte(int8u ch); void spi_send_byte(int8u); void send_buffer_to_pc(); void clear_buffer(); void delay(int16u d); void spi_read_from_slave(int8u n); //---------------------------------------------------------------- void main() { int32u i=0; led_mcu_start = 0; //work led on init_system(); //system initial spi_recevie = 0; //initial user flag rs232_point = 0; isp_point = 0; clear_buffer(); //empty buffer #ifdef master while (1) { if (ri) //check uart ri { ri = 0; if (rs232_point < buf_size) { buffer[rs232_point++] = sbuf //save uart rx data } i = 65000; //wait another data } if (i > 0) { i--; //check wait if (i == 0) //send all data at wait end { if (rs232_point > 0) { isp_point = 0; stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 361 www.stcmcu.com spi_status = 1; //1:spi send spdat = buffer[isp_point++]; //trigger spi send action while (isp_point < rs232_point); //other send in interrupt } delay(300); spi_read_from_slave(rs232_point); //read slave data send_buffer_to_pc(); //send back to pc clear_buffer(); spi_receive = 0; rs232_point = 0; isp_point = 0; ri = 0; } } } #else spi_receive = 0; spi_status = 0; //0:spi receive rs232_point = 0; isp_point = 0; while (1) { if (spi_recevie) { spi_receive = 0; i = 10000; //wait another data } if (i > 0) { i--; if (i == 0) { if (!spi_status) //spi receive { rs232_point = isp_point; isp_point = 0; send_buffer_to_pc(); //send buffer data to pc } isp_point = 0; spi_status = 1; //1:spi send spi_recevie = 0; while (!spi_receive); //wait send the 1 st data delay(50); //set timeout clear_buffer(); rs232_point = 0; isp_point = 0; spi_status = 0; //0:spi receive spi_recevie = 0; } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 362 www.stcmcu.com } } #endif } //---------------------------------------------------------------- void spi_interrupt_routine() interrupt spi_interrupt_vector { spi_buffer = spdat; //save spi data spstat = spif_wcol_mask; //clear spi flag spi_receive = 1; //set spi received flag if (spi_status) //1:spi send { if (isp_point < rs232_point) { spdat = buffer[isp_point]; isp_point++; } } else //0:spi receive { if (isp_point < buf_size) { buffer[isp_point] = spi_buffer; isp_point++; } } } //---------------------------------------------------------------- void initial_rs232() { es = 0; scon = 0x50; //uart mode(8-bit variable) tmod &= 0x0f; //timer0 mode(8-bit auto-reload) tmod |= 0x20; th1 = tl1 = 256 C fosc/384/baud; //uart baudrate tr1 = 1 auxr |= 0x40; //1t mode } //---------------------------------------------------------------- void initial_spi() { #ifdef master spctl = config_master; //master mode #else spctl = config_slave; //slave mode #endif spstat = spif_wcol_mask; //clear spi flag ie2 |= 0x02; //enable spi interrupt } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 363 www.stcmcu.com //---------------------------------------------------------------- void init_system() { initial_rs232(); //initial uart initial_spi(); //initial spi ea = 1; } //---------------------------------------------------------------- void rs232_send_byte(int8u ch) { ti = 0; //ready send sbuf = ch; //write uart data while (ti = 0); //wait data sent ti = 0; //clear tx flag } //---------------------------------------------------------------- void send_buffer_to_pc() //send all data in buffer to pc { int8u i; if (rs232_point == 0) return; rs232_send_byte(rs232_point); if (i=0; i mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 364 www.stcmcu.com //---------------------------------------------------------------- #ifdef masrer void spi_read_from_slave(int8u n) //receive slave data { int8u j; clear_buffer() spi_status = 0; //0:spi receive isp_point = 0; spi_receive = 0; spdat = 0x00; //trigger spi clock while (!spi_receive); spi_recevie = 0; isp_point = 0; //discard the 1 st data for (j=0; j mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 365 www.stcmcu.com chapter 12. iap / eeprom the isp in stc12c5a60s2 series makes it possible to update the users application program and non-volatile application data (in iap-memory) without removing the mcu chip from the actual end product. this useful capability makes a wide range of field-update applications possible. (note isp needs the loader program pre- programmed in the isp-memory.) in general, the user neednt know how isp operates because stc has provided the standard isp tool and embedded isp code in stc shipped samples.but, to develop a good program for isp function, the user has to understand the architecture of the embedded flash. the embedded flash consists of 16 pages. each page contains 512 bytes. dealing with flash, the user must erase it in page unit before writing (programming) data into it.erasing flash means setting the content of that flash as ffh. two erase modes are available in this chip. one is mass mode and the other is page mode. the mass mode gets more performance, but it erases the entire flash. the page mode is something performance less, but it is flexible since it erases flash in page unit. unlike rams real-time operation, to erase flash or to write (program) flash often takes long time so to wait finish. furthermore, it is a quite complex timing procedure to erase/program flash. fortunately, the stc12c5a60s2 series carried with convenient mechanism to help the user read/change the flash content. just filling the target address and data into several sfr, and triggering the built-in isp automation, the user can easily erase, read, and program the embedded flash. the in-application program feature is designed for user to read/write nonvolatile data flash. it may bring great help to store parameters those should be independent of power-up and power-done action. in other words, the user can store data in data flash memory, and after he shutting down the mcu and rebooting the mcu, he can get the original value, which he had stored in. the user can program the data flash according to the same way as isp program, so he should get deeper under - standing related to sfr iap_data, iap_addrl, iap_addrh, iap_cmd, iap_trig, and iap_contr. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 366 www.stcmcu.com symbol description address bit address and symbol msb lsb value after power-on or reset iap_data isp/iap flash data register c2h 1111 1111b iap_addrh isp/iap flash address high c3h 0000 0000b iap_addrl isp/iap flash address low c4h 0000 0000b iap_cmd isp/iap flash command register c5h - - - - - - ms1 ms0 xxxx x000b iap_trig isp/iap flash command trigger c6h xxxx xxxxb iap_contr isp/iap control register c7h iapen swbs swrst cmd_fail - wt2 wt1 wt0 0000 x000b pcon power control 87h smod smod0 lvdf pof gf1 gf0 pd idl 0011 0000b 12.1 iap / eeprom special function registers the following special function registers are related to the iap/isp/eeprom operation. all these registers can be accessed by software in the users application program. 1. isp/iap flash data register : iap_data (address: c2h, non bit-addressable) iap_data is the data port register for isp/iap operation. the data in iap_data will be written into the desired address in operating isp/iap write and it is the data window of readout in operating isp/ iap read. 2. isp/iap flash address registers : iap_addrh and iap_addrl iap_addrh is the high-byte address port for all isp/iap modes. iap_addrh[7:5] must be cleared to 000, if one bit of iap_addrh[7:5] is set, the iap/isp write function must fail. iap_addrl is the low port for all isp/iap modes. in page erase operation, it is ignored. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 367 www.stcmcu.com 3. isp/iap flash command register : iap_cmd (non bit -addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 iap_cmd c5h name - - - - - - ms1 ms0 b7~b2: reserved. ms1, ms0 : isp/iap operating mode selection. iap_cmd is used to select the flash mode for performing numerous isp/iap function or used to access protected sfrs. 0, 0 : standby 0, 1 : data flash/eeprom read. 1, 0 : data flash/eeprom program. 1, 1 : data flash/eeprom page erase. 4. isp/iap flash command trigger register : iap_trig (address: c6h, non bit -addressable) iap_trig is the command port for triggering isp/iap activity and protected sfrs access. if iap_trig is filled with sequential 0x5ah, 0xa5h and if iapen(iap_contr.7) = 1, isp/iap activity or protected sfrs access will triggered. 5. isp/iap control register : iap_contr (non bit-addressable) sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 iap_contr c7h name iapen swbs swrst cmd_fail - wt2 wt2 wt0 iapen : isp/iap operation enable. 0 : global disable all isp/iap program/erase/read function. 1 : enable isp/iap program/erase/read function. swbs: software boot selection control. 0 : boot from main-memory after reset. 1 : boot from isp memory after reset. swrst: software reset trigger control. 0 : no operation 1 : generate software system reset. it will be cleared by hardware automatically. cmd_fail: command fail indication for isp/iap operation. 0 : the last isp/iap command has finished successfully. 1 : the last isp/iap command fails. it could be caused since the access of flash memory was inhibited. b3: reserved. software must write 0 on this bit when iap_contr is written. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 368 www.stcmcu.com if power inputing source is 220v ac, then the reference application circuit as bellow: 470f + vcc mcu p4.6/lvd gnd 7805 + 104 r1 r2 note : 7805 output 8.5v voltage and use r1 and r2 can achieve the low voltage detect function at low threshold voltage. program can use query mode or interrupt mode to check lvdf flag. the detailed implementation is clear lvdf at first and then read lvdf again, if lvdf is still 1, then maybe low voltage, you should save data immediately. after saved completed, check lvdf continue. if lvdf is 1 then wait for voltage restoration, else if lvdf is 0, then you can go other function code. wt2~wt0 : waiting time selection while flash is busy . setting wait times cpu wait times wt2 wt1 wt0 read (2 system clocks) program (=55us) sector erase (=21ms) recommended system clock frequency (mhz) 1 1 1 2 sysclks 55 sysclks 21012 sysclks 1mhz 1 1 0 2 sysclks 110 sysclks 42024 sysclks 2mhz 1 0 1 2 sysclks 165 sysclks 63036 sysclks 3mhz 1 0 0 2 sysclks 330 sysclks 126072 sysclks 6mhz 0 1 1 2 sysclks 660 sysclks 252144 sysclks 12mhz 0 1 0 2 sysclks 1100 sysclks 420240 sysclks 20mhz 0 0 1 2 sysclks 1320 sysclks 504288 sysclks 24mhz 0 0 0 2 sysclks 1760 sysclks 672384 sysclks 30mhz note: software reset actions could reset other sfr,but it never influences bits iapen and swbs.the iapen and swbs. the iapen and swbs only will be reset by power-up action, while not software reset. 6. when the operation voltage is too low, eeprom / iap function should be disabled sfr name address bit b7 b6 b5 b4 b3 b2 b1 b0 pcon 87h name smod smod0 lvdf pof gf1 gf0 pd idl lvdf : pin low-voltage flag. once low voltage condition is detected (vcc power is lower than lvd voltage), it is set by hardware (and should be cleared by software). stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 369 www.stcmcu.com 12.2 stc12c5a60s2 series internal eeprom allocation table stc12c5a60s2 series microcontroller's data flash (internal available eeprom) address (and program space is separate) : if the application area of iap write data/erase sector of the action, the statements will be ignore and continue to the next one. program in user application area (ap area), only operate iap/isp on data flash (eeprom ) iap12c5a60s2 / ad/pwm and iap12le5a60s2 / ad/pwm are excepted, this several types in the application area can modify the application stc12c5a60s2/ad/pwm series mcu internal eeprom selection table type eeprom (byte) sector numbers begin_sector begin_address end_sector end_address stc12c5a08s2/ad/pwm 8k 16 0000h 1fffh stc12c5a16s2/ad/pwm 8k 16 0000h 1fffh stc12c5a20s2/ad/pwm 8k 16 0000h 1fffh stc12c5a32s2/ad/pwm 28k 56 0000h 6fffh stc12c5a40s2/ad/pwm 20k 40 0000h 4fffh stc12c5a48s2/ad/pwm 12k 24 0000h 2fffh stc12c5a52s2/ad/pwm 8k 16 0000h 1fffh stc12c5a56s2/ad/pwm 4k 8 0000h 0fffh stc12c5a60s2/ad/pwm 1k 2 0000h 03ffh stc12le5a60s2/ad/pwm series mcu internal eeprom selection table type eeprom (byte) sector numbers begin_sector begin_address end_sector end_address stc12le5a08s2/ad/pwm 8k 16 0000h 1fffh stc12le5a16s2/ad/pwm 8k 16 0000h 1fffh stc12le5a20s2/ad/pwm 8k 16 0000h 1fffh stc12le5a32s2/ad/pwm 28k 56 0000h 6fffh stc12le5a40s2/ad/pwm 20k 40 0000h 4fffh stc12le5a48s2/ad/pwm 12k 24 0000h 2fffh stc12le5a52s2/ad/pwm 8k 16 0000h 1fffh stc12le5a56s2/ad/pwm 4k 8 0000h 0fffh stc12le5a60s2/ad/pwm 1k 2 0000h 03ffh the following series are special. user can modify the application the application area, all flash area can be modified as eeprom iap12c5a62s2/ad/pwm - 124 0000h f7ffh iap12le5a62s2/ad/pwm - 124 0000h f7ffh stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 370 www.stcmcu.com stc12c5a32s2/ad/pwm address reference table in detail (512 bytes per sector) stc12le5a32s2/ad/pwm address reference table in detail (512 bytes per sector) sector 1 sector 2 sector 3 sector 4 each sector 512 byte suggest the same t i m e s m o d i f i e d data in the same sector, each times modified data in different sectors, don't have to use full, of course, it was all to use start end start end start end start end 0000h 01ffh 0200h 03ffh 0400h 05ffh 0600h 07ffh sector 5 sector 6 sector7 sector 8 start end start end start end start end 0800h 09ffh 0a00h 0bffh 0c00h 0dffh 0e00h 0fffh sector 9 sector 10 sector 11 sector 12 start end start end start end start end 1000h 11ffh 1200h 13ffh 1400h 15ffh 1600h 17ffh sector 13 sector 14 sector 15 sector 16 start end start end start end start end 1800h 19ffh 1a00h 1bffh 1c000h 1dffh 1e00h 1fffh sector 17 sector 18 sector 19 sector 20 start end start end start end start end 2000h 21ffh 2200h 23ffh 2400h 25ffh 2600h 27ffh sector 21 sector 22 sector 23 sector 24 start end start end start end start end 2800h 29ffh 2a00h 2bffh 2c00h 2dffh 2e00h 2fffh sector 25 sector 26 sector 27 sector 28 start end start end start end start end 3000h 31ffh 3200h 33ffh 3400h 35ffh 3600h 37ffh sector 29 sector 30 sector 31 sector 32 start end start end start end start end 3800h 39ffh 3a00h 3bffh 3c000h 3dffh 3e00h 3fffh sector 33 sector 34 sector 35 sector 36 start end start end start end start end 4000h 41ffh 4200h 43ffh 4400h 45ffh 4600h 47ffh sector 37 sector 38 sector 39 sector 40 start end start end start end start end 4800h 49ffh 4a00h 4bffh 4c00h 4dffh 4e00h 4fffh sector 41 sector 42 sector 43 sector 44 start end start end start end start end 5000h 51ffh 5200h 53ffh 5400h 55ffh 5600h 57ffh sector 45 sector 46 sector 47 sector 48 start end start end start end start end 5800h 59ffh 5a00h 5bffh 5c00h 5dffh 5e00h 5fffh sector 49 sector 50 sector 51 sector 52 start end start end start end start end 6000h 61ffh 6200h 63ffh 6400h 65ffh 6600h 67ffh sector 53 sector 54 sector 55 sector 56 start end start end start end start end 6800h 69ffh 6a00h 6bffh 6c00h 6dffh 6e00h 6fffh stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 371 www.stcmcu.com 12.3 iap/eeprom assembly language program introduction ; /*it is decided by the assembler/compiler used by users that whether the sfrs addresses are declared by the data or the equ directive*/ iap_data data 0c2h or iap_data equ 0c2h iap_addrh data 0c3h or iap_addrh equ 0c3h iap_addrl data 0c4h or iap_addrl equ 0c4h iap_cmd data 0c5h or iap_cmd equ 0c5h iap_trig data 0c6h or iap_trig equ 0c6h iap_contr data 0c7h or iap_contr equ 0c7h ;/*define isp/iap/eeprom command and wait time*/ isp_iap_byte_read equ 1 ;byte-read isp_iap_byte_program equ 2 ;byte-program isp_iap_sector_erase equ 3 ;sector-erase wait_time equ 0 ;set wait time ;/*byte-read*/ mov iap_addrh, #byte_addr_high ;set isp/iap/eeprom address high mov iap_addrl, #byte_addr_low ;set isp/iap/eeprom address low mov iap_contr, #wait_time ;set wait time orl iap_contr, #10000000b ;open isp/iap function mov iap_cmd, #isp_iap_byte_read ;set isp/iap byte-read command mov iap_trig, #5ah ;send trigger command1 (0x5a) mov iap_trig, #0a5h ;send trigger command2 (0xa5) nop ;cpu will hold here until isp/iap/eeprom operation complete mov a, iap_data ;read isp/iap/eeprom data ;/*disable isp/iap/eeprom function, make mcu in a safe state*/ mov iap_contr, #00000000b ;close isp/iap/eeprom function mov iap_cmd, #00000000b ;clear isp/iap/eeprom command ;mov iap_trig, #00000000b ;clear trigger register to prevent mistrigger ;mov iap_addrh, #0ffh ;move 00 into address high-byte unit, ;data ptr point to non-eeprom area ;mov iap_addrl, #0ffh ;move 00 into address low-byte unit, ;prevent misuse ;/*byte-program, if the byte is null(0ffh), it can be programmed; else, mcu must operate sector-erase firstly, and then can operate byte-program.*/ mov iap_data, #one_data ;write isp/iap/eeprom data mov iap_addrh, #byte_addr_high ;set isp/iap/eeprom address high mov iap_addrl, #byte_addr_low ;set isp/iap/eeprom address low mov iap_contr, #wait_time ;set wait time orl iap_contr, #10000000b ;open isp/iap function mov iap_cmd, #isp_iap_byte_read ;set isp/iap byte-read command mov iap_trig, #5ah ;send trigger command1 (0x5a) mov iap_trig, #0a5h ;send trigger command2 (0xa5) nop ;cpu will hold here until isp/iap/eeprom operation complete stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 372 www.stcmcu.com ;/*disable isp/iap/eeprom function, make mcu in a safe state*/ mov iap_contr, #00000000b ;close isp/iap/eeprom function mov iap_cmd, #00000000b ;clear isp/iap/eeprom command ;mov iap_trig, #00000000b ;clear trigger register to prevent mistrigger ;mov iap_addrh, #ffh ;move 00h into address high-byte unit, ;data ptr point to non-eeprom area ;mov iap_addrl, #0ffh ;move 00h into address low-byte unit, ;prevent misuse ;/*erase one sector area, there is only sector-erase instead of byte-erase, every sector area account for 512 bytes*/ mov iap_addrh, #sectot_first_byte_addr_high ;set the sector area starting address high mov iap_addrl, #sectot_first_byte_addr_low ;set the sector area starting address low mov iap_contr, #wait_time ;set wait time orl iap_contr, #10000000b ;open isp/iap function mov iap_cmd, #isp_iap_sector_erase ;set sectot-erase command mov iap_trig, #5ah ;send trigger command1 (0x5a) mov iap_trig, #0a5h ;send trigger command2 (0xa5) nop ;cpu will hold here until isp/iap/eeprom operation complete ;/*disable isp/iap/eeprom function, make mcu in a safe state*/ mov iap_contr, #00000000b ;close isp/iap/eeprom function mov iap_cmd, #00000000b ;clear isp/iap/eeprom command ;mov iap_trig, #00000000b ;clear trigger register to prevent mistrigger ;mov iap_addrh, #0ffh ;move 00h into address high-byte unit, ; data ptr point to non-eeprom area ;mov iap_addrl, #0ffh ;move 00h into address low-byte unit, ;prevent misuse stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 373 www.stcmcu.com little common sense: (stc mcu data flash use as eeprom function) three basic commands -- bytes read, byte programming, the sector erased byte programming: "1" write "1" or "0", will "0" write "0".just ffh can byte programming. if the byte not ffh, you must erase the sector , because only the "sectors erased" to put "0" into "1". sector erased: only "sector erased" will also be a "0" erased for "1". big proposal: 1. the same times modified data in the same sector, not the same times modified data in other sectors, won't have to read protection. 2. if a sector with only one byte, that's real eeprom, stc mcu data flash faster than external eeprom, read a byte/many one byte programming is about 2 clock / 55us. 3. if in a sector of storing a large amounts of data, a only need to modify one part of a byte, or when the other byte don't need to modify data must first read on stc mcu, then erased ram the whole sector, again will need to keep data and need to amend data in bytes written back to this sector section literally only bytes written orders (without continuous bytes, write command). then each sector use bytes are using the less the convenient (not need read a lot of maintained data). frequently asked questions: 1. iap instructions after finishing, address is automatically "add 1" or "minus 1"? answer: not 2. send 5a and a5 after iap ordered the trigger whether to have sent 5a and a5 trigger? answer: yes stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 374 www.stcmcu.com 12.4 eeprom demo program (c and asm) 1. c code listing /*-------------------------------------------------------------------------------*/ /* --- stc mcu international limited ----------------------------------*/ /* --- stc 1t series mcu isp/iap/eeprom demo ----------------*/ /* --- mobile: (86)13922809991 -----------------------------------------*/ /* --- fax: 86-755-82905966 ---------------------------------------------*/ /* --- tel: 86-755-82948412 ----------------------------------------------*/ /* --- web: www.stcmcu.com -----------------------------------------*/ /* if you want to use the program or the program referenced in the */ /* article, please specify in which data and procedures from stc */ /*-------------------------------------------------------------------------------*/ #include "reg51.h" #include "intrins.h" typedef unsigned char byte; typedef unsigned int word; /*declare sfr associated with the iap */ sfr iap_data = 0xc2; //flash data register sfr iap_addrh = 0xc3; //flash address high sfr iap_addrl = 0xc4; //flash address low sfr iap_cmd = 0xc5; //flash command register sfr iap_trig = 0xc6; //flash command trigger sfr iap_contr = 0xc7; //flash control register / *define isp/iap/eeprom command*/ #define cmd_idle 0 //stand-by #define cmd_read 1 //byte-read #define cmd_program 2 //byte-program #define cmd_erase 3 //sector-erase /*define isp/iap/eeprom operation const for iap_contr*/ //#define enable_iap 0x80 //if sysclk<30mhz //#define enable_iap 0x81 //if sysclk<24mhz #define enable_iap 0x82 //if sysclk<20mhz //#define enable_iap 0x83 //if sysclk<12mhz //#define enable_iap 0x84 //if sysclk<6mhz //#define enable_iap 0x85 //if sysclk<3mhz //#define enable_iap 0x86 //if sysclk<2mhz //#define enable_iap 0x87 //if sysclk<1mhz //start address for stc12c5a60s2 eeprom #define iap_address 0x0000 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 375 www.stcmcu.com void delay(byte n); void iapidle(); byte iapreadbyte(word addr); void iapprogrambyte(word addr, byte dat); void iaperasesector(word addr); void main() { word i; p1 = 0xfe; //1111,1110 system reset ok delay(10); //delay iaperasesector(iap_address); //erase current sector for (i=0; i<512; i++) //check whether all sector data is ff { if (iapreadbyte(iap_address+i) != 0xff) goto error; //if error, break } p1 = 0xfc; //1111,1100 erase successful delay(10); //delay for (i=0; i<512; i++) //program 512 bytes data into data flash { iapprogrambyte(iap_address+i, (byte)i); } p1 = 0xf8; //1111,1000 program successful delay(10); //delay for (i=0; i<512; i++) //verify 512 bytes data { if (iapreadbyte(iap_address+i) != (byte)i) goto error; //if error, break } p1 = 0xf0; //1111,0000 verify successful while (1); error: p1 &= 0x7f; //0xxx,xxxx iap operation fail while (1); } /*---------------------------- software delay function ----------------------------*/ stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 376 www.stcmcu.com void delay(byte n) { word x; while (n--) { x = 0; while (++x); } } /*---------------------------- disable isp/iap/eeprom function make mcu in a safe state ----------------------------*/ void iapidle() { iap_contr = 0; //close iap function iap_cmd = 0; //clear command to standby iap_trig = 0; //clear trigger register iap_addrh = 0x80; //data ptr point to non-eeprom area iap_addrl = 0; //clear iap address to prevent misuse } /*---------------------------- read one byte from isp/iap/eeprom area input: addr (isp/iap/eeprom address) output:flash data ----------------------------*/ byte iapreadbyte(word addr) { byte dat; //data buffer iap_contr = enable_iap; //open iap function, and set wait time iap_cmd = cmd_read; //set isp/iap/eeprom read command iap_addrl = addr; //set isp/iap/eeprom address low iap_addrh = addr >> 8; //set isp/iap/eeprom address high iap_trig = 0x5a; //send trigger command1 (0x5a) iap_trig = 0xa5; //send trigger command2 (0xa5) _nop_(); //mcu will hold here until isp/iap/eeprom //operation complete dat = iap_data; //read isp/iap/eeprom data iapidle(); //close isp/iap/eeprom function return dat; //return flash data } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 377 www.stcmcu.com /*---------------------------- program one byte to isp/iap/eeprom area input: addr (isp/iap/eeprom address) dat (isp/iap/eeprom data) output:- ----------------------------*/ void iapprogrambyte(word addr, byte dat) { iap_contr = enable_iap; //open iap function, and set wait time iap_cmd = cmd_program; //set isp/iap/eeprom program command iap_addrl = addr; //set isp/iap/eeprom address low iap_addrh = addr >> 8; //set isp/iap/eeprom address high iap_data = dat; //write isp/iap/eeprom data iap_trig = 0x5a; //send trigger command1 (0x5a) iap_trig = 0xa5; //send trigger command2 (0xa5) _nop_(); //mcu will hold here until isp/iap/eeprom //operation complete iapidle(); } /*---------------------------- erase one sector area input: addr (isp/iap/eeprom address) output:- ----------------------------*/ void iaperasesector(word addr) { iap_contr = enable_iap; //open iap function, and set wait time iap_cmd = cmd_erase; //set isp/iap/eeprom erase command iap_addrl = addr; //set isp/iap/eeprom address low iap_addrh = addr >> 8; //set isp/iap/eeprom address high iap_trig = 0x5a; //send trigger command1 (0x5a) iap_trig = 0xa5; //send trigger command2 (0xa5) _nop_(); //mcu will hold here until isp/iap/eeprom //operation complete iapidle(); } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 378 www.stcmcu.com 2. assembly code listing ;/*--------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited -----------------------------------*/ ;/* --- stc 1t series mcu isp/iap/eeprom demo -----------------*/ ;/* --- mobile: (86)13922809991 ------------------------------------------*/ ;/* --- fax: 86-755-82905966 ----------------------------------------------*/ ;/* --- tel: 86-755-82948412 -----------------------------------------------*/ ;/* --- web: www.stcmcu.com -----------------------------------------*/ ;/* if you want to use the program or the program referenced in the */ ;/* article, please specify in which data and procedures from stc */ ;/*------------------------------------------------------------------------------*/ ;/*declare sfrs associated with the iap */ iap_data equ 0c2h ;flash data register iap_addrh equ 0c3h ;flash address high iap_addrl equ 0c4h ;flash address low iap_cmd equ 0c5h ;flash command register iap_trig equ 0c6h ;flash command trigger iap_contr equ 0c7h ;flash control register ;/*define isp/iap/eeprom command*/ cmd_idle equ 0 ;stand-by cmd_read equ 1 ;byte-read cmd_program equ 2 ;byte-program cmd_erase equ 3 ;sector-erase ;/*define isp/iap/eeprom operation const for iap_contr*/ ;enable_iap equ 80h ;if sysclk<30mhz ;enable_iap equ 81h ;if sysclk<24mhz enable_iap equ 82h ;if sysclk<20mhz ;enable_iap equ 83h ;if sysclk<12mhz ;enable_iap equ 84h ;if sysclk<6mhz ;enable_iap equ 85h ;if sysclk<3mhz ;enable_iap equ 86h ;if sysclk<2mhz ;enable_iap equ 87h ;if sysclk<1mhz ;//start address for stc12c5a60s2 eeprom iap_address equ 0000h ;----------------------------------------- org 0000h ljmp main ;----------------------------------------- stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 379 www.stcmcu.com org 0100h main: mov p1, #0feh ;1111,1110 system reset ok lcall delay ;delay ;------------------------------- mov dptr, #iap_address ;set isp/iap/eeprom address lcall iap_erase ;erase current sector ;------------------------------- mov dptr, #iap_address ;set isp/iap/eeprom address mov r0, #0 ;set counter (512) mov r1, #2 check1: ;check whether all sector data is ff lcall iap_read ;read flash cjne a, #0ffh, error ;if error, break inc dptr ;inc flash address djnz r0, check1 ;check next djnz r1, check1 ;check next ;------------------------------- mov p1, #0fch ;1111,1100 erase successful lcall delay ;delay ;------------------------------- mov dptr, #iap_address ;set isp/iap/eeprom address mov r0, #0 ;set counter (512) mov r1, #2 mov r2, #0 ;initial test data next: ;program 512 bytes data into data flash mov a, r2 ;ready iap data lcall iap_program ;program flash inc dptr ;inc flash address inc r2 ;modify test data djnz r0, next ;program next djnz r1, next ;program next ;------------------------------- mov p1, #0f8h ;1111,1000 program successful lcall delay ;delay ;------------------------------- mov dptr, #iap_address ;set isp/iap/eeprom address mov r0, #0 ;set counter (512) mov r1, #2 mov r2, #0 check2: ;verify 512 bytes data lcall iap_read ;read flash cjne a, 2, error ;if error, break inc dptr ;inc flash address stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 380 www.stcmcu.com inc r2 ;modify verify data djnz r0, check2 ;check next djnz r1, check2 ;check next ;------------------------------- mov p1, #0f0h ;1111,0000 verify successful sjmp $ ;------------------------------- error: mov p0, r0 mov p2, r1 mov p3, r2 clr p1.7 ;0xxx,xxxx iap operation fail sjmp $ ;/*---------------------------- ;software delay function ;----------------------------*/ delay: clr a mov r0, a mov r1, a mov r2, #20h delay1: djnz r0, delay1 djnz r1, delay1 djnz r2, delay1 ret ;/*---------------------------- ;disable isp/iap/eeprom function ;make mcu in a safe state ;----------------------------*/ iap_idle: mov iap_contr, #0 ;close iap function mov iap_cmd, #0 ;clear command to standby mov iap_trig, #0 ;clear trigger register mov iap_addrh, #80h ;data ptr point to non-eeprom area mov iap_addrl, #0 ;clear iap address to prevent misuse ret ;/*---------------------------- ;read one byte from isp/iap/eeprom area ;input: dptr(isp/iap/eeprom address) ;output:acc (flash data) ;----------------------------*/ iap_read: mov iap_contr, #enable_iap ;open iap function, and set wait time mov iap_cmd, #cmd_read ;set isp/iap/eeprom read command stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 381 www.stcmcu.com mov iap_addrl, dpl ;set isp/iap/eeprom address low mov iap_addrh, dph ;set isp/iap/eeprom address high mov iap_trig, #5ah ;send trigger command1 (0x5a) mov iap_trig, #0a5h ;send trigger command2 (0xa5) nop ;mcu will hold here until isp/iap/eeprom operation complete mov a, iap_data ;read isp/iap/eeprom data lcall iap_idle ;close isp/iap/eeprom function ret ;/*---------------------------- ;program one byte to isp/iap/eeprom area ;input: dpat(isp/iap/eeprom address) ;acc (isp/iap/eeprom data) ;output:- ;----------------------------*/ iap_program: mov iap_contr, #enable_iap ;open iap function, and set wait time mov iap_cmd, #cmd_program ;set isp/iap/eeprom program command mov iap_addrl, dpl ;set isp/iap/eeprom address low mov iap_addrh, dph ;set isp/iap/eeprom address high mov iap_data, a ;write isp/iap/eeprom data mov iap_trig, #5ah ;send trigger command1 (0x5a) mov iap_trig, #0a5h ;send trigger command2 (0xa5) nop ;mcu will hold here until isp/iap/eeprom operation complete lcall iap_idle ;close isp/iap/eeprom function ret ;/*---------------------------- ;erase one sector area ;input: dptr(isp/iap/eeprom address) ;output:- ;----------------------------*/ iap_erase: mov iap_contr, #enable_iap ;open iap function, and set wait time mov iap_cmd, #cmd_erase ;set isp/iap/eeprom erase command mov iap_addrl, dpl ;set isp/iap/eeprom address low mov iap_addrh, dph ;set isp/iap/eeprom address high mov iap_trig, #5ah ;send trigger command1 (0x5a) mov iap_trig, #0a5h ;send trigger command2 (0xa5) nop ;mcu will hold here until isp/iap/eeprom operation complete lcall iap_idle ;close isp/iap/eeprom function ret end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 382 www.stcmcu.com chapter 13. stc12 series programming tools usage power-on,reset mcu frist running isp monitor code detect whether there ia a legitimate isp command download user program to ap area. reset to ap area running user code no yes 13.1 in-system-programming (isp) principle if need download code into stc12c5a60s2 series , p1.0 and p1.1 pin must be connected to gnd if you chose the "next program code, p1.0/1.1 need=0/0" option, then the next time you need to re-download the program, first of all must be connected p1.0 and p1.1 to gnd must be cold-reset (power-on reset),mcu will run from isp monitor code, for any warm-reset (includ e reset-pin, watchdog), mcu will run user code directly. wait isp command for tens or hundreds milliseconds, if no legitimate command, mcu will reset to ap area . pc application must send command at first then power on mcu stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 383 www.stcmcu.com 13.2 stc12c5a60s2 series application circuit for isp 1 2 3 4 5 6 7 8 16 15 14 13 12 11 10 9 vcc gnd t1out r1in r1out t1in t2in r2out c1+ v+ c1- c2+ c2- v- t2out r2in u1-p1.0 u1-p1.1 mcu-vcc u1-p3.0 u1-p3.1 gnd 0.1f vcc vcc gnd pc_rxd(com pin2) pc_txd(com pin3) 2 3 5 1k 1k vcc 104 c6 1k mcu_rxd(p3.0) mcu_txd(p3.1) c2<47pf c1<47pf x1 usb+5v t1out r1in gnd usb1 this part of the circuit has nothing to do with the downloads stc3232,stc232,max232,sp232 pc com notes: traditional 8051's ale pin regardless of whether access to external data bus, will have a clock frequency output. the signals is a source of interference to the system. for this reason,stc mcu new added a enable/ disable ale signal output switch, thus reduced mcu internal to external electromagnetic emissions, improve system stability and reliability. if needs the signal as other peripheral device's clock source, you can get clock source from clkout0/p3.4, clkout1/p3.5, clkout2/p1.0 or xtal2 clock output. (recommended a 200ohm series resistor to the xtal2 pin) 31 30 29 28 27 26 25 24 23 22 21 40 39 38 37 36 35 34 33 32 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ad3/p0.3 p1.0/adc0/clkout2 p1.1/adc1 p1.2/adc2/eci/rxd2 p1.3/adc3/ccp0/txd2 p1.4/adc4/ccp1/ss p1.5/adc5/mosi p1.6/adc6/miso p1.7/adc7/sclk rst/p4.7 p3.0/rxd/int p3.1/txd p3.4/t0/int/clkout0 p3.5/t1/int/clkout1 xtal2 xtal1 gnd vcc ad0/p0.0 ad1/p0.1 ad2/p0.2 ad4/p0.4 ad5/p0.5 ad6/p0.6 ad7/p0.7 rst2/lvd/p4.6 ale/p4.5 na/p4.4 ad15/p2.7 ad14/p2.6 ad13/p2.5 ad12/p2.4 ad11/p2.3 ad10/p2.2 ad9/p2.1 ad8/p2.0 p3.2/int0 p3.3/int1 p3.6/wr p3.7/rd + 10f 10k c1 r1 + vin sw1 power on 10f c5 + system power/usb +5v stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 384 www.stcmcu.com users in their target system, such as the p3.0/p3.1 through the rs-232 level shifter connected to the computer after the conversion of ordinary rs-232 serial port to connect the system programming / upgrading client software. if the user panel recommended no rs-232 level converter, should lead to a socket, with gnd/p3.1/ p3.0/vcc four signal lines, so that the user system can be programmed directly. of course, if the six signal lines can lead to gnd/p3.1/p3.0/vcc/p1.1/p1.0 as well, because you can download the program by p1.0/p1.1 isp ban. if you can gnd/p3.1/p3.0/vcc/p1.1/p1.0/reset seven signal lines leads to better, so you can easily use "offline download board (no computer)" . isp programming on the theory and application guide to see "stc12c5201ad series mcu development / programming tools help"section. in addition, we have standardized programming download tool, the user can then program into the goal in the above systems, you can borrow on top of it rs-232 level shifter connected to the computer to download the program used to do. programming a chip roughly be a few seconds, faster than the ordinary universal programmer much faster, there is no need to buy expensive third-party programmer?. pc stc-isp software downloaded from the website www.stcmcu.com stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 385 www.stcmcu.com 13.3 pc side application usage according to actual situation, the user selects the appropriate maximum baud rate in practice, if p3.0/ p3.1 already connected to a rs232/rs485 or other equipment, it is recommended that selection p1.0 / p1.1 = 0/0 can download options press this button when mass production all new settings are valid in the next power-on. enable the option in debugging stage stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 386 www.stcmcu.com step1 : select mcu type (e.g. stc12c5a60s2) step2 : load user program code (*.bin or *.hex) setp3 : select the serial port you are using setp4 : config the hardware option step5 : press isp programming or re-programming button to download user program note : must press isp programming or re-programming button first, then power on mcu, otherwise will cannot download. about hardware connection 1. mcu rxd (p3.0) ---- rs232 ---- pc com port txd (pin3) 2. mcu txd (p3.1) ---- rs232 ---- pc com port rxd (pin2) 3. mcu gng-------pc com port gnd (pin5) 4. rs232 : you can select stc232 / stc3232 / max232 / max3232 / using a demo board as a programmer stc-isp ver3.0a pcb can be welded into three kinds of circuits, respectively, support the stc's 16/20/28/32 pins mcu, the back plate of the download boards are affixed with labels,users need to pay special attention to. all the download board is welded 40-pin socket, the sockets 20-pin is ground line, all types of mcu should be put on the socket according to the way of alignment with the ground. the method of programming user code using download board as follow: 1. according to the type of mcu choose supply voltage, a. for 5v mcu, using jumper jp1 to connect mcu-vcc to +5v pin b. for 3v mcu, using jumper jp1 to connect mcu-vcc to +3.3v pin 2. download cable (provide by stc) a. connect db9 serial connector to the computer's rs-232 serial interface b. plug the usb interface at the same side into your computer's usb port for power supply c. connect the usb interface at the other side into stc download board 3. other interfaces do not need to connect. 4. in a non-pressed state to sw1, and mcu-vcc power led off. 5. for sw3 p1.0/p1.1 = 1/1 when sw3 is non-pressed p1.0/p1.1 = 0/0 when sw3 is pressed if you have select the next program code, p1.0/p1.1 need = 0/0 option, then sw3 must be in a pressed state 6. put target mcu into the u1 socket, and locking socket 7. press the download button in the pc side application 8. press sw1 switch in the download board 9. close the demo board power supply and remove the mcu after download successfully. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 387 www.stcmcu.com 13.4 compiler / assembler programmer and emulator about compiler/assembler any traditional compiler / assembler and the popular keil are suitable for stc mcu. for selection mcu body, the traditional compiler / assembler, you can choose intel's 8052 / 87c52 / 87c52 / 87c58 or philips's p87c52 / p87c54/p87c58 in the traditional environment, in keil environment, you can choose the types in front of the proposed or download the stc chips database file (stc.cdb) from the stc official website. about programmer you can use the stc specific isp programmer. (can be purchased from the stc or apply for free sample). programmer can be used as demo board about emulator we do not provite specific emulator now. if you have a traditional 8051 emulator, you can use it to simulate stc mcus some 8052 basic functions. 13.5 self-defined isp download demo /*-------------------------------------------------------------------------------------------------------------*/ /* --- stc mcu international limited ----------------------------------------------------------------*/ /* --- stc 1t series mcu using software to custom download code demo---------------------*/ /* --- mobile: (86)13922809991 ------------------------------------------------------------------------*/ /* --- fax: 86-755-82905966 ----------------------------------------------------------------------------*/ /* --- tel: 86-755-82948412 -----------------------------------------------------------------------------*/ /* --- web: www.stcmcu.com -----------------------------------------------------------------------*/ /* if you want to use the program or the program referenced in the ------------------------------*/ /* article, please specify in which data and procedures from stc ------------------------------*/ /*-------------------------------------------------------------------------------------------------------------*/ #include #include sfr iap_contr = 0xc7; sbit mcu_start_led = p1^7; #define self_define_isp_download_command 0x22 #define reload_count 0xfb //18.432mhz,12t,smod=0,9600bps //#define reload_count 0xf6 //18.432mhz,12t,smod=0,4800bps //#define reload_count 0xec //18.432mhz,12t,smod=0,2400bps //#define reload_count 0xd8 //18.432mhz,12t,smod=0,1200bps void serial_port_initial(void); void send_uart(unsigned char); void uart_interrupt_receive(void); void soft_reset_to_isp_monitor(void); void delay(void); void display_mcu_start_led(void); stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 388 www.stcmcu.com void main(void) { unsigned char i = 0; serial_port_initial(); //initial uart display_mcu_start_led(); //turn on the work led send_uart(0x34); //send uart test data send_uart(0xa7); // send uart test data while (1); } void send_uart(unsigned char i) { es = 0; //disable serial interrupt ti = 0; //clear ti flag sbuf = i; //send this data while (!ti); //wait for the data is sent ti = 0; //clear ti flag es = 1; //enable serial interrupt } void uart_interrupt)receive(void) interrupt 4 using 1 { unsigned char k = 0; if (ri) { ri = 0; k = sbuf; if (k == self_define_isp_command) //check the serial data { delay(); //delay 1s delay(); //delay 1s soft_reset_to_isp_monitor(); } } if (ti) { ti = 0; } } void soft_reset_to_isp_monitor(void) { iap_contr = 0x60; //0110,0000 soft reset system to run isp monitor } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 389 www.stcmcu.com void delay(void) { unsigned int j = 0; unsigned int g = 0; for (j=0; j<5; j++) { for (g=0; g<60000; g++) { _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); } } } void display_mcu_start_led(void) { unsigned char i = 0; for (i=0; i<3; i++) { mcu_start_led = 0; //turn on work led dejay(); mcu_start_led = 1; //turn off work led dejay(); mcu_start_led = 0; //turn on work led } } stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 390 www.stcmcu.com in addition, the pc-side application also need to make the following settings clicking the "help" button as show in above figure, we can see the detail explaination as below. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 391 www.stcmcu.com appendix a: assembly language programming introduction assembly language is a computer language lying between the extremes of machine language and high-level language like pascal or c use words and statements that are easily understood by humans, although still a long way from "natural" language.machine language is the binary language of computers.a machine language program is a series of binary bytes representing instructions the computer can execute. assembly language replaces the binary codes of machine language with easy to remember "mnemonics"that facilitate programming.for example, an addition instruction in machine language might be represented by the code "10110011".it might be represented in assembly language by the mnemonic "add".programming with mnemonics is obviously preferable to programming with binary codes. of course, this is not the whole story. instructions operate on data, and the location of the data is specified by various "addressing modes" emmbeded in the binary code of the machine language instruction. so, there may be several variations of the add instruction, depending on what is added. the rules for specifying these variations are central to the theme of assembly language programming. an assembly language program is not executable by a computer. once written, the program must undergo translation to machine language. in the example above, the mnemonic "add" must be translated to the binary code "10110011". depending on the complexity of the programming environment, this translation may involve one or more steps before an executable machine language program results. as a minimum, a program called an "assembler" is required to translate the instruction mnemonics to machine language binary codes. afurther step may require a "linker" to combine portions of program from separate files and to set the address in memory at which th program may execute. we begin with a few definitions. an assembly language program i a program written using labels, mnemonics, and so on, in which each statement corresponds to a machine instruction. assembly language programs, often called source code or symbolic code, cannot be executed by a computer. a machine language program is a program containing binary codes that represent instructions to a computer. machine language programs, often called object code, are executable by a computer. a assembler is a program that translate an assembly language program into a machine language program. the machine language program (object code) may be in "absolute" form or in "relocatable" form. in the latter case, "linking" is required to set the absolute address for execution. a linker is a program that combines relocatable object programs (modules) and produces an absolute object program that is executable by a computer. a linker is sometimes called a "linker/locator" to reflect its separate functions of combining relocatable modules (linking) and setting the address for execution (locating). a segment is a unit of code or data memory. a segment may be relocatable or absolute. a relocatable segment has a name, type, and other attributes that allow the linker to combine it with other paritial segments, if required, and to correctly locate the segment. an absolute segment has no name and cannot be combined with other segments. a module contains one or more segments or partial segments. a module has a name assigned by the user. the module definitions determine the scope of local symbols. an object file contains one or more modules. a module may be thought of as a "file" in many instances. a program consists of a single absolute module, merging all absolute and relocatable segments from all input modules. a program contains only the binary codes for instructions (with address and data constants) that are understood by a computer. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 392 www.stcmcu.com assembler operation there are many assembler programs and other support programs available to facilitate the development of applications for the 8051 microcontroller. intel's original mcs-51 family assembler, asm51, is no longer available commercially. however, it set the standard to which the others are compared. asm51 is a powerful assembler with all the bells and whistles. it is available on intel development systems and on the ibm pc family of microcomputers. since these "host" computers contain a cpu chip other than the 8051, asm51 is called a cross assembler. an 8051 source program may be written on the host computer (using any text editor) and may be assembled to an object file and listing file (using asm51), but the program may not be executed. since the host system's cpu chip is not an 8051, it does not understand the binary instruction in the object file. execution on the host computer requires either hardware emulation or software simulation of the target cpu. a third possibility is to download the object program to an 8051-based target system for execution. asm51 is invoked from the system prompt by asm51 source_file [assembler_controls] the source file is assembled and any assembler controls specified take effect. the assembler receives a source file as input (e.g., program.src) and generates an object file (program.obj) and listing file (program. lst) as output. this is illustrated in figure 1. since most assemblers scan the source program twice in performing the translation to machine language, they are described as two-pass assemblers. the assembler uses a location counter as the address of instructions and the values for labels. the action of each pass is described below. program.src asm51 program.obj program.lst figure 1 assembling a source program legend utility program user file pass one during the first pass, the source file is scanned line-by-line and a symbol table is built. the location counter defaults to 0 or is set by the org (set origin) directive. as the file is scanned, the location counter is incremented by the length of each instruction. define data directives (dbs or dws) increment the location counter by the number of bytes defined. reserve memory directives (dss) increment the location counter by the number of bytes reserved. each time a label is found at the beginning of a line, it is placed in the symbol table along with the current value of the location counter. symbols that are defined using equate directives (equs) are placed in the symbol table along with the "equated" value. the symbol table is saved and then used during pass two. pass two during pass two, the object and listing files are created. mnemonics are converted to opcodes and placed in the output files. operands are evaluated and placed after the instruction opcodes. where symbols appear in the operand field, their values are retrieved from the symbol table (created during pass one) and used in calculating the correct data or addresses for the instructions. since two passes are performed, the source program may use "forward references", that is, use a symbol before it is defined. this would occur, for example, in branching ahead in a program. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 393 www.stcmcu.com the object file, if it is absolute, contains only the binary bytes (00h-0fh) of the machine language program. a relocatable object file will also contain a sysmbol table and other information required for linking and locating. the listing file contains ascii text codes (02h-7eh) for both the source program and the hexadecimal bytes in the machine language program. a good demonstration of the distinction between an object file and a listing file is to display each on the host computer's crt display (using, for example, the type command on ms-dos systems). the listing file clearly displays, with each line of output containing an address, opcode, and perhaps data, followed by the program statement from the source file. the listing file displays properly because it contains only ascii text codes. displaying the object file is a problem, however. the output will appear as "garbage", since the object file contains binary codes of an 8051 machine language program, rather than ascii text codes. assembly language program format assembly language programs contain the following: machine instructions assembler directives assembler controls comments ? ? ? ? machine instructions are the familiar mnemonics of executable instructions (e.g., anl). assembler directives are instructions to the assembler program that define program structure, symbols, data, constants, and so on (e.g., org). assembler controls set assembler modes and direct assembly flow (e.g., $title). comments enhance the readability of programs by explaining the purpose and operation of instruction sequences. those lines containing machine instructions or assembler directives must be written following specific rules understood by the assembler. each line is divided into "fields" separated by space or tab characters. the general format for each line is as follows: [label:] mnemonic [operand] [, operand] [] [;commernt] only the mnemonic field is mandatory. many assemblers require the label field, if present, to begin on the left in column 1, and subsequent fields to be separated by space or tab charecters. with asm51, the label field needn't begin in column 1 and the mnemonic field needn't be on the same line as the label field. the operand field must, however, begin on the same line as the mnemonic field. the fields are described below. label field a label represents the address of the instruction (or data) that follows. when branching to this instruction, this label is usded in the operand field of the branch or jump instruction (e.g., sjmp skip). whereas the term "label" always represents an address, the term "symbol" is more general. labels are one type of symbol and are identified by the requirement that they must terminate with a colon(:). symbols are assigned values or attributes, using directives such as equ, segment, bit, data, etc. symbols may be addresses, data constants, names of segments, or other constructs conceived by the programmer. symbols do not terminate with a colon. in the example below, par is a symbol and start is a label (which is a type of symbol). par equ 500 ;"par" is a symbol which ;represents the value 500 start: mov a,#0ffh ;"start" is a label which ;represents the address of ;the mov instruction a symbol (or label) must begin with a letter, question mark, or underscore (_); must be followed by letters, digit, "?", or "_"; and can contain up to 31 characters. symbols may use upper- or lowercase characters, but they are treated the same. reserved words (mnemonics, operators, predefined symbols, and directives) may not be used. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 394 www.stcmcu.com mnemonic field intruction mnemonics or assembler directives go into mnemonic field, which follows the label field. examples of instruction mnemonics are add, mov, div, or inc. examples of assembler directives are org, equ, or db. operand field the operand field follows the mnemonic field. this field contains the address or data used by the instruction. a label may be used to represent the address of the data, or a symbol may be used to represent a data constant. the possibilities for the operand field are largely dependent on the operation. some operations have no operand (e.g., the ret instruction), while others allow for multiple operands separated by commas. indeed, the possibilties for the operand field are numberous, and we shall elaborate on these at length. but first, the comment field. comment field remarks to clarify the program go into comment field at the end of each line. comments must begin with a semicolon (;). each lines may be comment lines by beginning them with a semicolon. subroutines and large sections of a program generally begin with a comment blockserveral lines of comments that explain the general properties of the section of software that follows. special assembler symbols special assembler symbols are used for the register-specific addressing modes. these include a, r0 through r7, dptr, pc, c and ab. in addition, a dollar sign ($) can be used to refer to the current value of the location counter. some examples follow. setb c inc dptr jnb ti , $ the last instruction above makes effective use of asm51's location counter to avoid using a label. it could also be written as here: jnb ti , here indirect address for certain instructions, the operand field may specify a register that contains the address of the data. the commercial "at" sign (@) indicates address indirection and may only be used with r0, r1, the dptr, or the pc, depending on the instruction. for example, add a , @r0 movc a , @a+pc the first instruction above retrieves a byte of data from internal ram at the address specified in r0. the second instruction retrieves a byte of data from external code memory at the address formed by adding the contents of the accumulator to the program counter. note that the value of the program counter, when the add takes place, is the address of the instruction following movc. for both instruction above, the value retrieved is placed into the accumulator. immediate data instructions using immediate addressing provide data in the operand field that become part of the instruction. immediate data are preceded with a pound sign (#). for example, stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 395 www.stcmcu.com constant equ 100 mov a , #0feh orl 40h , #constant all immediate data operations (except mov dptr,#data) require eight bits of data. the immediate data are evaluated as a 16-bit constant, and then the low-byte is used. all bits in the high-byte must be the same (00h or ffh) or the error message "value will not fit in a byte" is generated. for example, the following instructions are syntactically correct: mov a , #0ff00h mov a , #00ffh but the following two instructions generate error messages: mov a , #0fe00h mov a , #01ffh if signed decimal notation is used, constants from -256 to +255 may also be used. for example, the following two instructions are equivalent (and syntactically correct): mov a , #-256 mov a , #0ff00h both instructions above put 00h into accumulator a. data address many instructions access memory locations using direct addressing and require an on-chip data memory address (00h to 7fh) or an sfr address (80h to 0ffh) in the operand field. predefined symbols may be used for the sfr addresses. for example, mov a , 45h mov a , sbuf ;same as mov a, 99h bit address one of the most powerful features of the 8051 is the ability to access individual bits without the need for masking operations on bytes. instructions accessing bit-addressable locations must provide a bit address in internal data memory (00h to 7fh) or a bit address in the sfrs (80h to 0ffh). there are three ways to specify a bit address in an instruction: (a) explicitly by giving the address, (b) using the dot operator between the byte address and the bit position, and (c) using a predefined assembler symbol. some examples follow. setb 0e7h ;explicit bit address setb acc.7 ;dot operator (same as above) jnb ti , $ ;"ti" is a pre-defined symbol jnb 99h , $ ;(same as above) code address a code address is used in the operand field for jump instructions, including relative jumps (sjmp and conditional jumps), absolute jumps and calls (acall, ajmp), and long jumps and calls (ljmp, lcall). the code address is usually given in the form of a label. asm51 will determine the correct code address and insert into the instruction the correct 8-bit signed offset, 11-bit page address, or 16-bit long address, as appropriate. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 396 www.stcmcu.com generic jumps and calls asm51 allows programmers to use a generic jmp or call mnemonic. "jmp" can be used instead of sjmp, ajmp or ljmp; and "call" can be used instead of acall or lcall. the assembler converts the generic mnemonic to a "real" instruction following a few simple rules. the generic mnemonic converts to the short form (for jmp only) if no forward references are used and the jump destination is within -128 locations, or to the absolute form if no forward references are used and the instruction following the jmp or call instruction is in the same 2k block as the destination instruction. if short or absolute forms cannot be used, the conversion is to the long form. the conversion is not necessarily the best programming choice. for example, if branching ahead a few instrucions, the generic jmp will always convert to ljmp even though an sjmp is probably better. consider the following assembled instructions sequence using three generic jumps. loc obj line source 1234 1 org 1234h 1234 04 2 start: inc a 1235 80fd 3 jmp start ;assembles as sjmp 12fc 4 org start + 200 12fc 4134 5 jmp start ;assembles as ajmp 12fe 021301 6 jmp finish ;assembles as ljmp 1301 04 7 finish: inc a 8 end the first jump (line 3) assembles as sjmp because the destination is before the jump ( i.e., no forward reference) and the offset is less than -128. the org directive in line 4 creates a gap of 200 locations between the label start and the second jump, so the conversion on line 5 is to ajmp because the offset is too great for sjmp. note also that the address following the second jump (12feh) and the address of start (1234h) are within the same 2k page, which, for this instruction sequence, is bounded by 1000h and 17ffh. this criterion must be met for absolute addressing. the third jump assembles as ljmp because the destination (finish) is not yet defined when the jump is assembled (i.e., a forward reference is used). the reader can verify that the conversion is as stated by examining the object field for each jump instruction. assemble-time expression evaluation values and constants in the operand field may be expressed three ways: (a) explicitly (e.g.,0efh), (b) with a predefined symbol (e.g., acc), or (c) with an expression (e.g.,2 + 3). the use of expressions provides a powerful technique for making assembly language programs more readable and more flexible. when an expression is used, the assembler calculates a value and inserts it into the instruction. all expression calculations are performed using 16-bit arithmetic; however, either 8 or 16 bits are inserted into the instruction as needed. for example, the following two instructions are the same: mov dptr, #04ffh + 3 mov dptr, #0502h ;entire 16-bit result used if the same expression is used in a "mov a,#data" instruction, however, the error message "value will not fit in a byte" is generated by asm51. an overview of the rules for evaluateing expressions follows. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 397 www.stcmcu.com number bases the base for numeric constants is indicated in the usual way for intel microprocessors. constants must be followed with "b" for binary, "o" or "q" for octal, "d" or nothing for decimal, or "h" for hexadecimal. for example, the following instructions are the same: mov a , #15h mov a , #1111b mov a , #0fh mov a , #17q mov a , #15d note that a digit must be the first character for hexadecimal constants in order to differentiate them from labels (i.e., "0a5h" not "a5h"). charater strings strings using one or two characters may be used as operands in expressions. the ascii codes are converted to the binary equivalent by the assembler. character constants are enclosed in single quotes ('). some examples follow. cjne a , # 'q', again subb a , # '0' ;convert ascii digit to binary digit mov dptr, # 'ab' mov dptr, #4142h ;same as above arithmetic operators the arithmetic operators are + addition - subtraction * multiplication / division mod modulo (remainder after division) for example, the following two instructions are same: mov a, 10 +10h mov a, #1ah the following two instructions are also the same: mov a, #25 mod 7 mov a, #4 since the mod operator could be confused with a symbol, it must be seperated from its operands by at least one space or tab character, or the operands must be enclosed in parentheses. the same applies for the other operators composed of letters. logical operators the logical operators are or logical or and logical and xor logical exclusive or not logical not (complement) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 398 www.stcmcu.com the operation is applied on the corresponding bits in each operand. the operator must be separated from the operands by space or tab characters. for example, the following two instructions are the same: mov a, # '9' and 0fh mov a, #9 the not operator only takes one operand. the following three mov instructions are the same: three equ 3 minus_three equ -3 mov a, # (not three) + 1 mov a, #minus_three mov a, #11111101b special operators the sepcial operators are shr shift right shl shift left high high-byte low low-byte () evaluate first for example, the following two instructions are the same: mov a, #8 shl 1 mov a, #10h the following two instructions are also the same: mov a, #high 1234h mov a, #12h relational operators when a relational operator is used between two operands, the result is alwalys false (0000h) or true (ffffh). the operators are eq = equals ne < > not equals lt < less than le <= less than or equal to gt > greater than ge >= greater than or equal to note that for each operator, two forms are acceptable (e.g., "eq" or "="). in the following examples, all relational tests are "true": mov a, #5 = 5 mov a,#5 ne 4 mov a,# 'x' lt 'z' mov a,# 'x' >= 'x' mov a,#$ > 0 mov a,#100 ge 50 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 399 www.stcmcu.com so, the assembled instructions are equal to mov a, #0ffh even though expressions evaluate to 16-bit results (i.e., 0ffffh), in the examples above only the low-order eight bits are used, since the instruction is a move byte operation. the result is not considered too big in this case, because as signed numbers the 16-bit value ffffh and the 8-bit value ffh are the same (-1). expression examples the following are examples of expressions and the values that result: expression result 'b' - 'a' 0001h 8/3 0002h 155 mod 2 0001h 4 * 4 0010h 8 and 7 0000h not 1 fffeh 'a' shl 8 4100h low 65535 00ffh (8 + 1) * 2 0012h 5 eq 4 0000h 'a' lt 'b' ffffh 3 <= 3 ffffhss a practical example that illustrates a common operation for timer initialization follows: put -500 into timer 1 registers th1 and tl1. in using the high and low operators, a good approach is value equ -500 mov th1, #high value mov tl1, #low value the assembler converts -500 to the corresponding 16-bit value (fe0ch); then the high and low operators extract the high (feh) and low (0ch) bytes. as appropriate for each mov instruction. operator precedence the precedence of expression operators from highest to lowest is ( ) high low * / mod shl shr + - eq ne lt le gt ge = < > < <= > >= not and or xor when operators of the same precedence are used, they are evaluated left to right. examples: expression value high ( 'a' shl 8) 0041h high 'a' shl 8 0000h not 'a' - 1 ffbfh 'a' or 'a' shl 8 4141h stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 400 www.stcmcu.com assembler directives assembler directives are instructions to the assembler program. they are not assembly language instructions executable by the target microprocessor. however, they are placed in the mnemonic field of the program. with the exception of db and dw, they have no direct effect on the contents of memory. asm51 provides several catagories of directives: assembler state control (org, end, using) symbol definition (segment, equ, set, data, idata, xdata, bit, code) storage initialization/reservation (ds, dbit, db, dw) program linkage (public, extrn,name) segment selection (rseg, cseg, dseg, iseg, eseg, xseg) ? ? ? ? ? each assembler directive is presented below, ordered by catagory. assembler state control org (set origin) the format for the org (set origin) directive is org expression the org directive alters the location counter to set a new program origin for statements that follow. a label is not p ermitted. two examples follow. org 100h ;set location counter to 100h org ($ + 1000h) and 0f00h ;set to next 4k boundary the org directive can be used in any segment type. if the current segment is absolute, the value will be an absolute address in the current segment. if a relocatable segment is active, the value of the org expression is treated as an offset from the base address of the current instance of the segment. end the format of the end directive is end end should be the last statement in the source file. no label is permitted and nothing beyond the end statement is processed by the assembler. using the format of the end directive is using expression this directive informs asm51 of the currently active register bank. subsequent uses of the predefined symbolic register addresses ar0 to ar7 will convert to the appropriate direct address for the active register bank. consider the following sequence: using 3 push ar7 using 1 push ar7 the first push above assembles to push 1fh (r7 in bank 3), whereas the second push assembles to push 0fh (r7 in bank 1). note that using does not actually switch register banks; it only informs asm51 of the active bank. executing 8051 instructions is the only way to switch register banks. this is illustrated by modifying the example above as follows: stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 401 www.stcmcu.com mov psw, #00011000b ;select register bank 3 using 3 push ar7 ;assemble to push 1fh mov psw, #00001000b ;select register bank 1 using 1 push ar7 ;assemble to push 0fh symbol definition the symbol definition directives create symbols that represent segment, registers, numbers, and addresses. none of these directives may be preceded by a label. symbols defined by these directives may not have been previously defined and may not be redefined by any means. the set directive is the only exception. symbol definiton directives are d escribed below. segment the format for the segment directive is shown below. symbol segment segment_type the symbol is the name of a relocatable segment. in the use of segments, asm51 is more complex than conventional assemblers, which generally support only "code" and "data" segment types. however, asm51 defines additional segment types to accommodate the diverse memory spaces in the 8051. the following are the defined 8051 segment types (memory spaces): code (the code segment) xdata (the external data space) data (the internal data space accessible by direct addressing, 00hC07h) idata (the entire internal data space accessible by indirect addressing, 00hC07h) bit (the bit space; overlapping byte locations 20hC2fh of the internal data space) ? ? ? ? ? for example, the statement eprom segment code declares the symbol eprom to be a segment of type code. note that this statement simply declares what eprom is. to actually begin using this segment, the rseg directive is used (see below). equ (equate) the format for the equ directive is symbol equ expression the equ directive assigns a numeric value to a specified symbol name. the symbol must be a valid symbol name, and the expression must conform to the rules described earlier. the following are examples of the equ directive: n27 equ 27 ;set n27 to the value 27 here equ $ ;set "here" to the value of ;the location counter cr equ 0dh ;set cr (carriage return) to 0dh message: db 'this is a message' length equ $ - message ;"length" equals length of "message" other symbol definition directives the set directive is similar to the equ directive except the symbol may be redefined later, using another set directive. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 402 www.stcmcu.com the data, idata, xdata, bit, and code directives assign addresses of the corresponding segment type to a symbol. these directives are not essential. a similar effect can be achieved using the equ directive; if used, however, they evoke powerful type-checking by asm51. consider the following two directives and four instructions: flag1 equ 05h flag2 bit 05h setb flag1 setb flag2 mov flag1, #0 mov flag2, #0 the use of flag2 in the last instruction in this sequence will generate a "data segment address expected" error message from asm51. since flag2 is defined as a bit address (using the bit directive), it can be used in a set bit instruction, but it cannot be used in a move byte instruction. hence, the error. even though flag1 represents the same value (05h), it was defined using equ and does not have an associated address space. this is not an advantage of equ, but rather, a disadvantage. by properly defining address symbols for use in a specific memory space (using the directives bit, data, xdata,ect.), the programmer takes advantage of asm51's powerful type-checking and avoids bugs from the misuse of symbols. storage initialization/reservation the storage initialization and reservation directives initialize and reserve space in either word, byte, or bit units. the space reserved starts at the location indicated by the current value of the location counter in the currently active segment. these directives may be preceded by a label. the storage initialization/reservation directives are described below. ds (define storage) the format for the ds (define storage) directive is [label:] ds expression the ds directive reserves space in byte units. it can be used in any segment type except bit. the expression must be a valid assemble-time expression with no forward references and no relocatable or external references. when a ds statement is encountered in a program, the location counter of the current segment is incremented by the value of the expression. the sum of the location counter and the specified expression should not exceed the limitations of the current address space. the following statement create a 40-byte buffer in the internal data segment: dseg at 30h ;put in data segment (absolute, internal) length equ 40 buffer: ds lengrh ;40 bytes reserved the label buffer represents the address of the first location of reserved memory. for this example, the buffer begins at address 30h because "at 30h" is specified with dseg. the buffer could be cleared using the following instruction sequence: mov r7, #length mov r0, #buffer loop: mov @r0, #0 djnz r7, loop (continue) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 403 www.stcmcu.com to create a 1000-byte buffer in external ram starting at 4000h, the following directives could be used: xstart equ 4000h xlength equ 1000 xseg at xstart xbuffer: ds xlength this buffer could be cleared with the following instruction sequence: mov dptr, #xbuffer loop: clr a movx @dptr, a inc dptr mov a, dpl cjne a, #low (xbuffer + xlength + 1), loop mov a, dph cjne a, #high (xbuffer + xlength + 1), loop (continue) this is an excellent example of a powerful use of asm51's operators and assemble-time expressions. since an instruction does not exist to compare the data pointer with an immediate value, the operation must be fabricated from available instructions. two compares are required, one each for the high- and low-bytes of the dptr. furthermore, the compare-and-jump-if-not-equal instruction works only with the accumulator or a register, so the data pointer bytes must be moved into the accumulator before the cjne instruction. the loop terminates only when the data pointer has reached xbuffer + length + 1. (the "+1" is needed because the data pointer is incremented after the last movx instruction.) dbit the format for the dbit (define bit) directive is, [label:] dbit expression the dbit directive reserves space in bit units. it can be used only in a bit segment. the expression must be a valid assemble-time expression with no forward references. when the dbit statement is encountered in a program, the location counter of the current (bit) segment is incremented by the value of the expression. note that in a bit segment, the basic unit of the location counter is bits rather than bytes. the following directives creat three flags in a absolute bit segment: bseg ;bit segment (absolute) keflag: dbit 1 ;keyboard status prflag: dbit 1 ;printer status dkflag: dbit 1 ;disk status since an address is not specified with bseg in the example above, the address of the flags defined by dbit could be determined (if one wishes to to so) by examining the symbol table in the .lst or .m51 files. if the definitions above were the first use of bseg, then kbflag would be at bit address 00h (bit 0 of byte address 20h). if other bits were defined previously using bseg, then the definitions above would follow the last bit defined. db (define byte) the format for the db (define byte) directive is, [label:] db expression [, expression] [] the db directive initializes code memory with byte values. since it is used to actually place data constants in code memory, a code segment must be active. the expression list is a series of one or more byte values (each of which may be an expression) separated by commas. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 404 www.stcmcu.com the db directive permits character strings (enclosed in single quotes) longer than two characters as long as they are not part of an expression. each character in the string is converted to the corresponding ascii code. if a label is used, it is assigned the address of th first byte. for example, the following statements cseg at 0100h squares: db 0, 1, 4, 9, 16, 25 ;squares of numbers 0-5 message: db 'login:', 0 ;null-terminated character string when assembled, result in the following hexadecimal memory assignments for external code memory: address contents 0100 00 0101 01 0102 04 0103 09 0104 10 0105 19 0106 4c 0107 6f 0108 67 0109 69 010a 6e 010b 3a 010c 00 dw (define word) the format for the dw (define word) directive is [label:] dw expression [, expression] [] the dw directive is the same as the db directive except two memory locations (16 bits) are assigned for each data item. for example, the statements cseg at 200h dw $, 'a', 1234h, 2, 'bc' result in the following hexadecimal memory assignments: address contents 0200 02 0201 00 0202 00 0203 41 0204 12 0205 34 0206 00 0207 02 0208 42 0209 43 program linkage program linkage directives allow the separately assembled modules (files) to communicate by permitting intermodule references and the naming of modules. in the following discussion, a "module" can be considered a "file." (in fact, a module may encompass more than one file.) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 405 www.stcmcu.com public the format for the public (public symbol) directive is public symbol [, symbol] [] the public directive allows the list of specified symbols to known and used outside the currently assembled module. a symbol declared public must be defined in the current module. declaring it public allows it to be referenced in another module. for example, public inchar, outchr, inline, outstr extrn the format for the extrn (external symbol) directive is extrn segment_type (symbol [, symbol] [], ) the extrn directive lists symbols to be referenced in the current module that are defined in other modules. the list of external symbols must have a segment type associated with each symbol in the list. (the segment types are code, xdata, data, idata, bit, and number. number is a type-less symbol defined by equ.) the segment type indicates the way a symbol may be used. the information is important at link-time to ensure symbols are used properly in different modules. the public and extrn directives work together. consider the two files, main.src and messages. src. the subroutines hello and good_bye are defined in the module messages but are made available to other modules using the public directive. the subroutines are called in the module main even though they are not defined there. the extrn directive declares that these symbols are defined in another module. main.src: extrn code (hello, good_bye) call hello call good_bye end messages.src: public hello, good_bye hello: (begin subroutine) ret good_bye: (begin subroutine) ret end neither main.src nor messages.src is a complete program; they must be assembled separately and linked together to form an executable program. during linking, the external references are resolved with correct addresses inserted as the destination for the call instructions. name the format for the name directive is name module_name stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 406 www.stcmcu.com all the usual rules for symbol names apply to module names. if a name is not provided, the module takes on the file name (without a drive or subdirectory specifier and without an extension). in the absence of any use of the name directive, a program will contain one module for each file. the concept of "modules," therefore, is somewhat cumbersome, at least for relatively small programming problems. even programs of moderate size (encompassing, for example, several files complete with relocatable segments) needn't use the name directive and needn't pay any special attention to the concept of "modules." for this reason, it was mentioned in the definition that a module may be considered a "file," to simplify learning asm51. however, for very large programs (several thousand lines of code, or more), it makes sense to partition the problem into modules, where, for example, each module may encompass several files containing routines having a common purpose. segment selection directives when the assembler encounters a segment selection directive, it diverts the following code or data into the selected segment until another segment is selected by a segment selection directive. the directive may select may select a previously defined relocatable segment or optionally create and select absolute segments. rseg (relocatable segment) the format for the rseg (relocatable segment) directive is rseg segment_name where "segment_name" is the name of a relocatable segment previously defined with the segment directive. rseg is a "segment selection" directive that diverts subsequent code or data into the named segment until another segment selection directive is encountered. selecting absolute segments rseg selects a relocatable segment. an "absolute" segment, on the other hand, is selected using one of the directives: cseg (at address) dseg (at address) iseg (at address) bseg (at address) xseg (at address) these directives select an absolute segment within the code, internal data, indirect internal data, bit, or external data address spaces, respectively. if an absolute address is provided (by indicating "at address"), the assembler terminates the last absolute address segment, if any, of the specified segment type and creates a new absolute segment starting at that address. if an absolute address is not specified, the last absolute segment of the specified type is continuted. if no absolute segment of this type was previously selected and the absolute address is omitted, a new segment is created starting at location 0. forward references are not allowed and start addresses must be absolute. each segment has its own location counter, which is always set to 0 initially. the default segment is an absolute code segment; therefore, the initial state of the assembler is location 0000h in the absolute code segment. when another segment is chosen for the first time, the location counter of the former segment retains the last active value. when that former segment is reselected, the location counter picks up at the last active value. the org directive may be used to change the location counter within the currently selected segment. assembler controls assembler controls establish the format of the listing and object files by regulating the actions of asm51. for the most part, assembler controls affect the look of the listing file, without having any affect on the program itself. they can be entered on the invocation line when a program is assembled, or they can be placed in the source file. assembler controls appearing in the source file must be preceded with a dollor sign and must begin in column 1. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 407 www.stcmcu.com there are two categories of assembler controls: primary and general. primary controls can be placed in the invocation line or at the beginnig of the source program. only other primary controls may precede a primary control. general controls may be placed anywhere in the source program. linker operation in developing large application programs, it is common to divide tasks into subprograms or modules containing sections of code (usually subroutines) that can be written separately from the overall program. the term "modular programming" refers to this programming strategy. generally, modules are relocatable, meaning they are not intended for a specific address in the code or data space. a linking and locating program is needed to combine the modules into one absolute object module that can be executed. intel's rl51 is a typical linker/locator. it processes a series of relocatable object modules as input and creates an executable machine language program (program, perhaps) and a listing file containing a memory map and symbol table (program.m51). this is illustrated in following figure. file3.obj file2.obj file1.obj rl51 program.abs program.map linker operation legend utility program user file as relocatable modules are combined, all values for external symbols are resolved with values inserted into the output file. the linker is invoked from the system prompt by rl51 input_list [t0 output_file] [location_controls] the input_list is a list of relocatable object modules (files) separated by commas. the output_list is the name of the output absolute object module. if none is supplied, it defaults to the name of the first input file without any suffix. the location_controls set start addresses for the named segments. for example, suppose three modules or files (main.obj, messages.obj, and subroutines.obj) are to be combined into an executable program (example), and that these modules each contain two relocatable segments, one called eprom of type code, and the other called onchip of type data. suppose further that the code segment is to be executable at address 4000h and the data segment is to reside starting at address 30h (in internal ram). the following linker invocation could be used: rs51 main.obj, messages.obj, subroutines.obj to example & code (eprom (4000h) data (onchip (30h)) note that the ampersand character "&" is used as the line continuaton character. if the program begins at the label start, and this is the first instruction in the main module, then execution begins at address 4000h. if the main module was not linked first, or if the label start is not at the beginning of main, then the program's entry point can be determined by examining the symbol table in the listing file example.m51 created by rl51. by default, example.m51 will contain only the link map. if a symbol table is desired, then each source program must have used the sdebug control. the following table shows the assembler controls supported by asm51. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 408 www.stcmcu.com assembler controls supported by asm51 name primary/ general default abbrev. meaning date (date) p date( ) da place string in header (9 char. max.) debug p nodebug db outputs debug symbol information to object file eject g not applicable ej continue listing on next page errorprint (file) p noerrorprint ep designates a file to receive error messages in addition to the listing file (defauts to console) noerrorprint p noerrorprint noep designates that error messages will be printed in listing file only gen g genonly go list only the fully expanded source as if all lines generated by a macro call were already in the source file genonly g genonly noge list only the original source text in the listing file inclued(file) g not applicable ic designates a file to be included as part of the program list g list li print subsequent lines of source code in listing file nolist g list noli do not print subsequent lines of source code in lisitng file macro (men_precent) p macro(50) mr evaluate and expand all macro calls. allocate percentage of free memory for macro processing nomacro p macro(50) nomr do not evalutate macro calls mod51 p mod51 mo recognize the 8051-specific predefined special function registers nomod51 p mod51 nomo do not recognize the 8051-specific predefined special function registers object(file) p object(source.obj) oj designates file to receive object code noobject p object(source.obj) nooj designates that no object file will be created paging p paging pi designates that listing file be broken into pages and each will have a header nopaging p paging nopi designates that listing file will contain no page breaks pagelength (n) p pagelengt(60) pl sets maximun number of lines in each page of listing file (range=10 to 65536) page width (n) p pagewidth(120) pw set maximum number of characters in each line of listing file (range = 72 to 132) print(file) p print(source.lst) pr designates file to receive source listing noprint p print(source.lst) nopr designates that no listing file will be created save g not applicable sa stores current control settings from save stack restore g not applicable rs restores control settings from save stack registerbank (rb,...) p registerbank(0) rb indicates one or more banks used in program module noregister- bank p registerbank(0) norb indicates that no register banks are used symbols p symbols sb creates a formatted table of all symbols used in program nosymbols p symbols nosb designates that no symbol table is created title(string) g title( ) tt places a string in all subsequent page headers (max.60 characters) workfiles (path) p same as source wf designates alternate path for temporay workfiles xref p noxref xr creates a cross reference listing of all symbols used in program noxref p noxref noxr designates that no cross reference list is created stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 409 www.stcmcu.com macros the macro processing facility (mpl) of asm51 is a "string replacement" facility. macros allow frequently used sections of code be defined once using a simple mnemonic and used anywhere in the program by inserting the mnemonic. programming using macros is a powerful extension of the techniques described thus far. macros can be defined anywhere in a source program and subsequently used like any other instruction. the syntax for macro definition is %*define (call_pattern) (macro_body) once defined, the call pattern is like a mnemonic; it may be used like any assembly language instruction by placing it in the mnemonic field of a program. macros are made distinct from "real" instructions by preceding them with a percent sign, "%". when the source program is assembled, everything within the macro-body, on a character-by-character basis, is substituted for the call-pattern. the mystique of macros is largely unfounded. they provide a simple means for replacing cumbersome instruction patterns with primitive, easy-to-remember mnemonics. the substitution, we reiterate, is on a character-by-character basisnothing more, nothing less. for example, if the following macro definition appears at the beginning of a source file, %*define (push_dptr) (push dph push dpl ) then the statement %push_dptr will appear in the .lst file as push dph push dpl the example above is a typical macro. since the 8051 stack instructions operate only on direct addresses, pushing the data pointer requires two push instructions. a similar macro can be created to pop the data pointer. there are several distinct advantages in using macros: a source program using macros is more readable, since the macro mnemonic is generally more indicative of the intended operation than the equivalent assembler instructions. the source program is shorter and requires less typing. using macros reduces bugs usi ng macros frees the programmer from dealing with low-level details. the last two points above are related. once a macro is written and debugged, it is used freely without the worry of bugs. in the push_dptr example above, if push and pop instructions are used rather than push and pop macros, the programmer may inadvertently reverse the order of the pushes or pops. (was it the high-byte or low- byte that was pushed first?) this would create a bug. using macros, however, the details are worked out once when the macro is writtenand the macro is used freely thereafter, without the worry of bugs. since the replacement is on a character-by-character basis, the macro definition should be carefully constructed with carriage returns, tabs, ect., to ensure proper alignment of the macro statements with the rest of the assembly language program. some trial and error is required. there are advanced features of asm51's macro-processing facility that allow for parameter passing, local labels, repeat operations, assembly flow control, and so on. these are discussed below. ? ? ? ? stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 410 www.stcmcu.com p arameter passing a macro with parameters passed from the main program has the following modified format: %*define (macro_name (parameter_list)) (macro_body) for example, if the following macro is defined, %*define (cmpa# (value)) (cjne a, #%value, $ + 3 ) then the macro call %cmpa# (20h) will expand to the following instruction in the .lst file: cjne a, #20h, $ + 3 although the 8051 does not have a "compare accumulator" instruction, one is easily created using the cjne instruction with "$+3" (the next instruction) as the destination for the conditional jump. the cmpa# mnemonic may be easier to remember for many programmers. besides, use of the macro unburdens the programmer from remembering notational details, such as "$+3." let's develop another example. it would be nice if the 8051 had instructions such as jump if accumulator greater than x jump if accumulator greater than or equal to x jump if accumulator less than x jump if accumulator less than or equal to x but it does not. these operations can be created using cjne followed by jc or jnc, but the details are tricky. suppose, for example, it is desired to jump to the label greater_than if the accumulator contains an ascii code greater than "z" (5ah). the following instruction sequence would work: cjne a, #5bh, $3 jnc greater_than the cjne instruction subtracts 5bh (i.e., "z" + 1) from the content of a and sets or clears the carry flag accordingly. cjne leaves c=1 for accumulator values 00h up to and including 5ah. (note: 5ah-5bh<0, therefore c=1; but 5bh-5bh=0, therefore c=0.) jumping to greater_than on the condition "not carry" correctly jumps for accumulator values 5bh, 5ch, 5dh, and so on, up to ffh. once details such as these are worked out, they can be simplified by inventing an appropriate mnemonic, defining a macro, and using the macro i nstead of the corresponding instruction sequence. here's the definition for a "jump if greater than" macro: %*define (jgt (value, label)) (cjne a, #%value+1, $+3 ;jgt jnc %label ) to test if the accumulator contains an ascii code greater than "z," as just discussed,the macro would be called as %jgt ('z', greater_than) asm51 would expand this into cjne a, #5bh, $+3 ;jgt jnc greater_than the jgt macro is an excellent example of a relevant and powerful use of macros. by using macros, the programmer benefits by using a meaningful mnemonic and avoiding messy and potentially bug-ridden details. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 411 www.stcmcu.com local labels local labels may be used within a macro using the following format: %*define (macro_name [(parameter_list)]) [local list_of_local_labels] (macro_body) for example, the following macro definition %*define (dec_dptr) local skip (dec dpl ;decrement data pointer mov a, dpl cjne a, #0ffh, %skip dec dpl %skip: ) would be called as %dec_dptr and would be expanded by asm51 into dec dpl ;decrement data pointer mov a, dpl cjne a, #0ffh, skip00 dec dph skip00: note that a local label generally will not conflict with the same label used elsewhere in the source program, since asm51 appends a numeric code to the local label when the macro is expanded. furthermore, the next use of the same local label receives the next numeric code, and so on. the macro above has a potential "side effect." the accumulator is used as a temporary holding place for dpl. if the macro is used within a section of code that uses a for another purpose, the value in a would be lost. this side effect probably represents a bug in the program. the macro definition could guard against this by saving a on the stack. here's an alternate definition for the dec_dptr macro: %*define (dec_dptr) local skip (pushacc dec dpl ;decrement data pointer mov a, dpl cjne a, #0ffh, %skip dec dph %skip: pop acc ) repeat operations this is one of several built-in (predefined) macros. the format is %repeat (expression) (text) for example, to fill a block of memory with 100 nop instructions, %repeat (100) (nop ) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 412 www.stcmcu.com control flow operations the conditional assembly of section of code is provided by asm51's control flow macro definition. the format is %if (expression) then (balanced_text) [else (balanced_text)] fi for example, intrenal equ 1 ;1 = 8051 serial i/o drivers ;0 = 8251 serial i/o drivers . . %if (internal) then (inchar: . ;8051 drivers . outchr: . . ) else (inchar: . ;8251 drivers . outchr: . . ) in this example, the symbol internal is given the value 1 to select i/o subroutines for the 8051's serial port, or the value 0 to select i/o subroutines for an external uart, in this case the 8251. the if macro causes asm51 to assemble one set of drivers and skip over the other. elsewhere in the program, the inchar and outchr subroutines are used without consideration for the particular hardware configuration. as long as the program as assembled with the correct value for internal, the correct subroutine is executed. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 413 www.stcmcu.com appendix b: 8051 c programming advantages and disadvantages of 8051 c the advantages of programming the 8051 in c as compared to assembly are: offers all the benefits of high-level, structured programming languages such as c, including the ease of writing subroutines often relieves the programmer of the hardware details that the complier handles on behalf of the programmer easier to write, especially for large and complex programs produces more readable program source codes nevertheless, 8051 c, being very similar to the conventional c language, also suffers from the following disadvantages: processes the disadvantages of high-level, structured programming languages. generally generates larger machine codes programmer has less control and less ability to directly interact with hardware to compare between 8051 c and assembly language, consider the solutions to the examplewrite a program using timer 0 to create a 1khz square wave on p1.0. a solution written below in 8051 c language: sbit portbit = p1^0; /*use variable portbit to refer to p1.0*/ main ( ) { tmod = 1; while (1) { th0 = 0xfe; tl0 = 0xc; tr0 = 1; while (tf0 !=1); tr0 = 0; tf0 = 0; portbit = !(p1.^0); } } a solution written below in assembly language: org 8100h mov tmod, #01h ;16-bit timer mode loop: mov th0, #0feh ;-500 (high byte) mov tl0, #0ch ;-500 (low byte) setb tr0 ;start timer wait: jnb tf0, wait ;wait for overflow clr tr0 ;stop timer clr tf0 ;clear timer overflow flag cpl p1.0 ;toggle port bit sjmp loop ;repeat end ? ? ? ? ? ? ? stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 414 www.stcmcu.com notice that both the assembly and c language solutions for the above example require almost the same number of lines. however, the difference lies in the readability of these programs. the c version seems more human than assembly, and is hence more readable. this often helps facilitate the human programmer's efforts to write even very complex programs. the assembly language version is more closely related to the machine code, and though less readable, often results in more compact machine code. as with this example, the resultant machine code from the assembly version takes 83 bytes while that of the c version requires 149 bytes, an increase of 79.5%! the human programmer's choice of either high-level c language or assembly language for talking to the 8051, whose language is machine language, presents an interesting picture, as shown in following figure. human language eg. english, malay, chinese machine language eg. 10011101 0101010101 complier assembler c (high-level) language eg. for (x=0; x<9; x++)... assembly language eg. mov, add, sub conversion between human, high-level, assembly, and machine language 8051 c compilers we saw in the above figure that a complier is needed to convert programs written in 8051 c language into machine language, just as an assembler is needed in the case of programs written in assembly language. a complier basically acts just like an assembler, except that it is more complex since the difference between c and machine language is far greater than that between assembly and machine language. hence the complier faces a greater task to bridge that difference. currently, there exist various 8051 c complier, which offer almost similar functions. all our examples ? n ? pro ? r ? ms h ? ve been ? ompile ? ? n ? teste ? with keil's vision 2 ide by keil softw ? re, ? n inte ? r ? te ? 8051 program development envrionment that includes its c51 cross compiler for c. a cross compiler is a compiler that normally runs on a platform such as ibm compatible pcs but is meant to compile programs into codes to be run on other platforms such as the 8051. data types 8051 c is very much like the conventional c language, except that several extensions and adaptations have been made to make it suitable for the 8051 programming environment. the first concern for the 8051 c programmer is the data types. recall that a data type is something we use to store data. readers will be familiar with the basic c data types such as int, char, and float, which are used to create variables to store integers, characters, or floating- points. in 8051 c, all the basic c data types are supported, plus a few additional data types meant to be used specifically with the 8051. the following table gives a list of the common data types used in 8051 c. the ones in bold are the specific 8051 extensions. the data type bit can be used to declare variables that reside in the 8051's bit-addressable locations (namely byte locations 20h to 2fh or bit locations 00h to 7fh). obviously, these bit variables can only store bit values of either 0 or 1. as an example, the following c statement: bit flag = 0; declares a bit variable called flag and initializes it to 0. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 415 www.stcmcu.com data types used in 8051 c language data type bits bytes value range bit 1 0 to 1 signed char 8 1 -128 to +127 unsigned char 8 1 0 to 255 enum 16 2 -32768 to +32767 signed short 16 2 -32768 to +32767 unsigned short 16 2 0 to 65535 signed int 16 2 -32768 to +32767 unsigned int 16 2 0 to 65535 signed long 32 4 -2,147,483,648 to +2,147,483,647 unsigned long 32 4 0 to 4,294,967,295 float 32 4 1.175494e-38 to 3.402823e+38 sbit 1 0 to 1 sfr 8 1 0 to 255 sfr16 16 2 0 to 65535 the data type sbit is somewhat similar to the bit data type, except that it is normally used to declare 1-bit variables that reside in special function registes (sfrs). for example: sbit p = 0xd0; declares the sbit variable p and specifies that it refers to bit address d0h, which is really the lsb of the psw sfr. notice the difference here in the usage of the assignment ("=") operator. in the context of sbit declarations, it indicatess what address the sbit variable resides in, while in bit declarations, it is used to specify the initial value of the bit variable. besides directly assigning a bit address to an sbit variable, we could also use a previously defined sfr variable as the base address and assign our sbit variable to refer to a certain bit within that sfr . for example: sfr psw = 0xd0; sbit p = psw^0; this declares an sfr variable called psw that refers to the byte address d0h and then uses it as the base address to refer to its lsb (bit 0). this is then assigned to an sbit variable, p. for this purpose, the carat symbol (^) is used to specify bit position 0 of the psw. a third alternative uses a constant byte address as the base address within which a certain bit is referred. as a n illustration, the previous two statements can be replaced with the following: sbit p = 0xd0 ^ 0; meanwhile, the sfr data type is used to declare byte (8-bit) variables that are associated with sfrs. the s tatement: sfr ie = 0xa8; declares an sfr variable ie that resides at byte address a8h. recall that this address is where the interrupt enable (ie) sfr is located; therefore, the sfr data type is just a means to enable us to assign names for sfrs so that it is easier to remember. the sfr16 data type is very similar to sfr but, while the sfr data type is used for 8-bit sfrs, sfr16 is used for 16-bit sfrs. for example, the following statement: sfr16 dptr = 0x82; stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 416 www.stcmcu.com declares a 16-bit variable dptr whose lower-byte address is at 82h. checking through the 8051 architecture, we find that this is the address of the dpl sfr, so again, the sfr16 data type makes it easier for us to refer to the sfrs by name rather than address. there's just one thing left to mention. when declaring sbit , sfr , or sfr16 variables, remember to do so outside main, otherwise you will get an error. in actual fact though, all the sfrs in the 8051, including the individual flag, status, and control bits in the bit-addressable sfrs have already been declared in an include file, called reg51.h, which comes packaged with most 8051 c compilers. by using reg51.h, we can refer for instance to the interrupt enable register as simply ie rather than having to specify the address a8h, and to the data pointer as dptr rather than 82h. all this makes 8051 c programs more human-readable and manageable. the contents of reg51.h are listed below. /*-------------------------------------------------------------------------------------------------------------------- reg51.h header file for generic 8051 microcontroller. -----------------------------------------------------------------------------------------------------------------------*/ sbit ie1 = 0x8b; sbit it1 = 0x8a; sbit ie0 = 0x89; sbit it0 = 0x88; /* ie */ sbit ea = 0xaf; sbit es = 0xac; sbit et1 = 0xab; sbit ex1 = 0xaa; sbit et0 = 0xa9; sbit ex0 = 0xa8; /* ip */ sbit ps = 0xbc; sbit pt1 = 0xbb; sbit px1 = 0xba; sbit pt0 = 0xb9; sbit px0 = 0xb8; /* p3 */ sbit rd = 0xb7; sbit wr = 0xb6; sbit t1 = 0xb5; sbit t0 = 0xb4; sbit int1 = 0xb3; sbit int0 = 0xb2; sbit txd = 0xb1; sbit rxd = 0xb0; /* scon */ sbit sm0 = 0x9f; sbit sm1 = 0x9e; sbit sm2 = 0x9d; sbit ren = 0x9c; sbit tb8 = 0x9b; sbit rb8 = 0x9a; sbit ti = 0x99; sbit ri = 0x98; /* byte register */ sfr p0 = 0x80; sfr p1 = 0x90; sfr p2 = 0xa0; sfr p3 = 0xb0; sfr psw = 0xd0; sfr acc = 0xe0; sfr b = 0xf0; sfr sp = 0x81; sfr dpl = 0x82; sfr dph = 0x83; sfr pcon = 0x87; sfr tcon = 0x88; sfr tmod = 0x89; sfr tl0 = 0x8a; sfr tl1 = 0x8b; sfr th0 = 0x8c; sfr th1 = 0x8d; sfr ie = 0xa8; sfr ip = 0xb8; sfr scon = 0x98; sfr sbuf = 0x99; /* bit register */ /* psw */ sbit cy = 0xd7; sbit ac = 0xd6; sbit f0 = 0xd5; sbit rs1 = 0xd4; sbit rs0 = 0xd3; sbit ov = 0xd2; sbit p = 0xd0; /* tcon */ sbit tf1 = 0x8f; sbit tr1 = 0x8e; sbit tf0 = 0x8d; sbit tr0 = 0x8c; stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 417 www.stcmcu.com memory types and models the 8051 has various types of memory space, including internal and external code and data memory. when declaring variables, it is hence reasonable to wonder in which type of memory those variables would reside. for this purpose, several memory type specifiers are available for use, as shown in following table. memory types used in 8051 c language memory type description (size) code code memory (64 kbytes) data directly addressable internal data memory (128 bytes) idata indirectly addressable internal data memory (256 bytes) bdata bit-addressable internal data memory (16 bytes) xdata external data memory (64 kbytes) pdata paged external data memory (256 bytes) the first memory type specifier given in above table is code . this is used to specify that a variable is to reside in code memory, which has a range of up to 64 kbytes. for example: char code errormsg[ ] = "an error occurred" ; declares a char array called errormsg that resides in code memory. if you want to put a variable into data memory, then use either of the remaining five data memory specifiers in above table. though the choice rests on you, bear in mind that each type of data memory affect the speed of access and the size of available data memory. for instance, consider the following declarations: signed int data num1; bit bdata numbit; unsigned int xdata num2; the first statement creates a signed int variable num1 that resides in inernal data memory (00h to 7fh). the next line declares a bit variable numbit that is to reside in the bit-addressable memory locations (byte addresses 20h to 2fh), also known as bdata . finally, the last line declares an unsigned int variable called num2 that resides in external data memory, xdata . having a variable located in the directly addressable internal data memory speeds up access considerably; hence, for programs that are time-critical, the variables should be of type data . for other variants such as 8052 with internal data memory up to 256 bytes, the idata specifier may be used. note however that this is slower than data since it must use indirect addressing. meanwhile, if you would rather have your variables reside in external memory, you have the choice of declaring them as pdata or xdata . a variable declared to be in pdata resides in the first 256 bytes (a page) of external memory, while if more storage is required, xdata should be used, which allows for accessing up to 64 kbytes of external data memory. what if when declaring a variable you forget to explicitly specify what type of memory it should reside in, or you wish that all variables are assigned a default memory type without having to specify them one by one? in this case, we make use of memory models . the following table lists the various memory models that you can use. memory models used in 8051 c language memory model description small variables default to the internal data memory (data) compact variables default to the first 256 bytes of external data memory (pdata) large variables default to external data memory (xdata) stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 418 www.stcmcu.com a program is explicitly selected to be in a certain memory model by using the c directive, #pragma. otherwise, the default memory model is small . it is recommended that programs use the small memory model as it allows for the fastest possible access by defaulting all variables to reside in internal data memory. the compact memory model causes all variables to default to the first page of external data memory while the large memory model causes all variables to default to the full external data memory range of up to 64 kbytes. arrays often, a group of variables used to store data of the same type need to be grouped together for better readability. for example, the ascii table for decimal digits would be as shown below. ascii table for decimal digits decimal digit ascii code in hex 0 30h 1 31h 2 32h 3 33h 4 34h 5 35h 6 36h 7 37h 8 38h 9 39h to store such a table in an 8051 c program, an array could be used. an array is a group of variables of the same data type, all of which could be accessed by using the name of the arrary along with an appropriate index. the array to store the decimal ascii table is: int table [10] = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39}; notice that all the elements of an array are separated by commas. to access an individul element, an index starting from 0 is used. for instance, table[0] refers to the first element while table[9] refers to the last element in this ascii table. structures sometime it is also desired that variables of different data types but which are related to each other in some way be grouped together. for example, the name, age, and date of birth of a person would be stored in different types of variables, but all refer to the person's personal details. in such a case, a structure can be declared. a structure is a group of related variables that could be of different data types. such a structure is declared by: struct person { char name; int age; long dob; }; once such a structure has been declared, it can be used like a data type specifier to create structure variables that have the member's name, age , and dob. for example: struct person grace = {"grace", 22, 01311980}; stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 419 www.stcmcu.com would create a structure variable grace to store the name, age, and data of birth of a person called grace. then in order to access the specific members within the person structure variable, use the variable name followed by the dot operator (.) and the member name. therefore, grace.name, grace.age, grace.dob would refer to grace's name, age, and data of birth, respectively. pointers when programming the 8051 in assembly, sometimes register such as r0, r1, and dptr are used to store the addresses of some data in a certain memory location. when data is accessed via these registers, indirect addressing is used. in this case, we say that r0, r1, or dptr are used to point to the data, so they are essentially pointers. correspondingly in c, indirect access of data can be done through specially defined pointer variables. point- ers are simply just special types of variables, but whereas normal variables are used to directly store data, pointer variables are used to store the addresses of the data. just bear in mind that whether you use normal variables or pointer variables, you still get to access the data in the end. it is just whether you go directly to where it is stored and get the data, as in the case of normal variables, or first consult a directory to check the location of that data before going there to get it, as in the case of pointer variables. declaring a pointer follows the format: data_type *pointer_name; where data_type refers to which type of data that the pointer is pointing to * denotes that this is a pointer variable pointer_name is the name of the pointer as an example, the following declarations: int * numptr int num; numptr = # first declares a pointer variable called numptr that will be used to point to data of type int. the second declaration declares a normal variable and is put there for comparison. the third line assigns the address of the num variable to the numptr pointer. the address of any variable can be obtained by using the address operator, &, as is used in this example. bear in mind that once assigned, the numptr pointer contains the address of the num variable, not the value of its data. the above example could also be rewritten such that the pointer is straightaway initialized with an address when it is first declared: int num; int * numptr = # in order to further illustrate the difference between normal variables and pointer variables, consider the following, which is not a full c program but simply a fragment to illustrate our point: int num = 7; int * numptr = # printf ("%d\n", num); printf ("%d\n", numptr); printf ("%d\n", &num); printf ("%d\n", *numptr); stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 420 www.stcmcu.com the first line declare a normal variable, num, which is initialized to contain the data 7. next, a pointer variable, numptr, is declared, which is initialized to point to the address of num. the next four lines use the printf( ) function, which causes some data to be printed to some display terminal connected to the serial port. the first such line displays the contents of the num variable, which is in this case the value 7. the next displays the contents of the numptr pointer, which is really some weird-looking number that is the address of the num variable. the third such line also displays the addresss of the num variable because the address operator is used to obtain num's address. the last line displays the actual data to which the numptr pointer is pointing, which is 7. the * symbol is called the indirection operator, and when used with a pointer, indirectly obtains the data whose address is pointed to by the pointer. therefore, the output display on the terminal would show: 7 13452 (or some other weird-looking number) 13452 (or some other weird-looking number) 7 a pointer's memory type recall that pointers are also variables, so the question arises where they should be stored. when declaring pointers, we can specify different types of memory areas that these pointers should be in, for example: int * xdata numptr = & num; this is the same as our previous pointer examples. we declare a pointer numptr, which points to data of type int stored in the num variable. the difference here is the use of the memory type specifier xdata after the *. this is specifies that pointer numptr should reside in external data memory ( xdata ), and we say that the pointer's memory type is xdata . typed pointers we can go even further when declaring pointers. consider the example: int data * xdata numptr = # the above statement declares the same pointer numptr to reside in external data memory ( xdata ), and this pointer points to data of type int that is itself stored in the variable num in internal data memory ( data ). the memory type specifier, data , before the * specifies the data memory type while the memory type specifier, xdata , after the * specifies the pointer memory type. pointer declarations where the data memory types are explicitly specified are called typed pointers. typed pointers have the property that you specify in your code where the data pointed by pointers should reside. the size of typed pointers depends on the data memory type and could be one or two bytes. untyped pointers when we do not explicitly state the data memory type when declaring pointers, we get untyped pointers, which are generic pointers that can point to data residing in any type of memory. untyped pointers have the advantage that they can be used to point to any data independent of the type of memory in which the data is stored. all untyped pointers consist of 3 bytes, and are hence larger than typed pointers. untyped pointers are also generally slower because the data memory type is not determined or known until the complied program is run at runtime. the first byte of untyped pointers refers to the data memory type, which is simply a number according to the following table. the second and third bytes are,respectively,the higher-order and lower-order bytes of the address being pointed to. an untyped pointer is declared just like normal c, where: int * xdata numptr = # does not explicitly specify the memory type of the data pointed to by the pointer. in this case, we are using untyped pointers. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 421 www.stcmcu.com data memory type values stored in first byte of untyped pointers value data memory type 1 idata 2 xdata 3 pdata 4 data/bdata 5 code functions in programming the 8051 in assembly, we learnt the advantages of using subroutines to group together common and frequently used instructions. the same concept appears in 8051 c, but instead of calling them subroutines, we call them functions . as in conventional c, a function must be declared and defined. a function definition includes a list of the number and types of inputs, and the type of the output (return type), puls a description of the internal contents, or what is to be done within that function. the format of a typical function definition is as follows: return_type function_name (arguments) [memory] [reentrant] [interrupt] [using] { } where return_type refers to the data type of the return (output) value function_name is any name that you wish to call the function as arguments is the list of the type and number of input (argument) values memory refers to an explicit memory model (small, compact or large) reentrant refers to whether the function is reentrant (recursive) interrupt indicates that the function is acctually an isr using explicitly specifies which register bank to use consider a typical example, a function to calculate the sum of two numbers: int sum (int a, int b) { return a + b; } this function is called sum and takes in two arguments, both of type int. the return type is also int, meaning that the output (return value) would be an int. within the body of the function, delimited by braces, we see that the return value is basically the sum of the two agruments. in our example above, we omitted explicitly specifying the options: memory, reentrant, interrupt, and using. this means that the arguments passed to the function would be using the default small memory model, meaning that they would be stored in internal data memory. this function is also by default non-recursive and a normal function, not an isr. meanwhile, the default register bank is bank 0. parameter passing in 8051 c, parameters are passed to and from functions and used as function arguments (inputs). nevertheless, the technical details of where and how these parameters are stored are transparent to the programmer, who does not need to worry about these techinalities. in 8051 c, parameters are passed through the register or through memory. passing parameters through registers is faster and is the default way in which things are done. the registers used and their purpose are described in more detail below. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 422 www.stcmcu.com registers used in parameter passing number of argument char / 1-byte pointer int / 2-byte pointer long/float generic pointer 1 r7 r6 & r7 r4Cr7 r1Cr3 2 r5 r4 &r5 r4Cr7 3 r3 r2 & r3 since there are only eight registers in the 8051, there may be situations where we do not have enough regist- ers for parameter passing. when this happens, the remaining parameters can be passed through fixed memory loacations. to specify that all parameters will be passed via memory, the noregparms control directive is used. to specify the reverse, use the regparms control directive. return values unlike parameters, which can be passed by using either registers or memory locations, output values must be returned from functions via registers. the following table shows the registers used in returning different types of values from functions. registers used in returning values from functions return type register description bit carry flag (c) char/unsigned char/1-byte pointer r7 int/unsigned int/2-byte pointer r6 & r7 msb in r6, lsb in r7 long/unsigned long r4Cr7 msb in r4, lsb in r7 float r4Cr7 32-bit ieee format generic pointer r1Cr3 memory type in r3, msb in r2, lsb in r1 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 423 www.stcmcu.com appendix c: stc12c5axx series electrical characteristics absolute maximum ratings parameter symbol min max unit srotage temperature tst -55 +125 operating temperature (i) ta -40 +85 operating temperature (c) ta 0 +70 dc power supply (5v) vdd - vss -0.3 +5.5 v dc power supply (3v) vdd - vss -0.3 +3.6 v voltage on any pin - -0.3 vcc + 0.3 v dc specification (5v mcu) sym parameter specification test condition min. typ max. unit v dd operating voltage 3.3 5.0 5.5 v i pd power down current - < 0.1 - ua 5v i idl idle current - 3.0 - ma 5v i cc operating current - 4 20 ma 5v v il1 input low (p0,p1,p2,p3) - - 0.8 v 5v v ih1 input high (p0,p1,p2,p3) 2.0 - - v 5v v ih2 input high (reset) 2.2 - - v 5v i ol1 sink current for output low (p0,p1,p2,p3) - 20 - ma 5v@vpin=0.45v i oh1 sourcing current for output high (p0,p1,p2,p3) (quasi-output) 150 230 - ua 5v i oh2 sourcing current for output high (p0,p1,p2,p3) (push-pull, strong-output) - 20 - ma 5v@vpin=2.4v i il logic 0 input current (p0,p1,p2,p3) - - 50 ua vpin=0v i tl logic 1 to 0 transition current (p0,p1,p2,p3) 100 270 600 ua vpin=2.0v stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 424 www.stcmcu.com dc specification (3v mcu) sym parameter specification test condition min. typ max. unit v dd operating voltage 2.2 3.3 3.6 v i pd power down current - <0.1 - ua 3.3v i idl idle current - 2.0 - ma 3.3v i cc operating current - 4 10 ma 3.3v v il1 input low (p0,p1,p2,p3) - - 0.8 v 3.3v v ih1 input high (p0,p1,p2,p3) 2.0 - - v 3.3v v ih2 input high (reset) 2.2 - - v 3.3v i ol1 sink current for output low (p0,p1,p2,p3) - 20 - ma 3.3v@vpin=0.45v i oh1 sourcing current for output high (p0,p1,p2,p3) (quasi-output) 40 70 - ua 3.3v i oh2 sourcing current for output high (p0,p1,p2,p3) (push-pull) - 20 - ma 3.3v i il logic 0 input current (p0,p1,p2,p3) - 8 50 ua vpin=0v i tl logic 1 to 0 transition current (p0,p1,p2,p3) - 110 600 ua vpin=2.0v stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 425 www.stcmcu.com appendix d: program for indirect addressing inner 256b ram ; /*---------------------------------------------------------------------------------------------------------------*/ ;/* --- stc mcu international limited ------------------------------------------------------------------*/ ;/* --- stc 1t series mcu the inner 256b normal ram (indirect addressing) demo -----------*/ ;/* --- mobile: (86)13922809991 --------------------------------------------------------------------------*/ ;/* --- fax: 86-755-82905966 ------------------------------------------------------------------------------*/ ;/* --- tel: 86-755-82948412 -------------------------------------------------------------------------------*/ ;/* --- web: www.stcmcu.com -------------------------------------------------------------------------*/ ;/* if you want to use the program or the program referenced in the --------------------------------*/ ;/* article, please specify in which data and procedures from stc --------------------------------*/ ;/*--------------------------------------------------------------------------------------------------------------*/ test_const equ 5ah ;test_ram equ 03h org 0000h ljmp initial org 0050h initial: mov r0, #253 mov r1, #3h test_all_ram: mov r2, #0ffh test_one_ram: mov a, r2 mov @r1, a clr a mov a, @r1 cjne a, 2h, error_display djnz r2, test_one_ram inc r1 djnz r0, test_all_ram ok_display: mov p1, #11111110b wait1: sjmp wait1 error_display: mov a, r1 mov p1, a wait2: sjmp wait2 end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 426 www.stcmcu.com appendix e: using serial port expand i/o interface stc12c5a60s2 series mcu serial port mode0 can be used for expand io if uart is free in your application. uart mode0 is a synchronous shift register, the baudrate is fixed at fosc/12, rxd pin (p3.0) is the data i/o port, and txd pin (p3.1) is clock output port, data width is 8 bits, always sent / received the lowest bit at first. (1) using 74hc165 expand parallel input ports please refer to the following circuit which using 2 pcs 74hc165 to expand 16 input i/os h g f e d c b a 10 12 13 14 3 4 5 6 q h q h sin s/l cp 9 7 1 15 2 8 16 vcc 74hc165 h g f e d c b a 11 12 13 14 3 4 5 6 q h q h sin s/l cp 9 7 1 15 2 8 16 vcc 74hc165 12cxx p3.0 p3.1 p1.0 10 104 104 11 74hc165 is a 8-bit parallel input shift register, when s/l (shift/load) pin is falling to low level, the parallel port data is read into internal register, and now, if s/l is raising to high and clockdisable pin (15 pin) is low level, then clock signal from cp pin is enable. at this time register data will be output from the dh pin (9 pin) with the clock input. mov r7,#05h ;read 5 groups data mov r0,#20h ;set buffer address start: clr p1.0 ;s/l = 0, load port data setb p1.0 ;s/l = 1, lock data and enable clock mov r1,#02h ;2 bytes per group rxdat:mov scon,#00010000b ;set serial as mode 0 and enable receive data wait: jnb ri,wait ;wait for receive complete clr ri ;clear receive complete flag mov a,sbuf ;read data to acc mov @r0,a ;save data to buffer inc r0 ;modify buffer ptr djnz r1,rxdat ;read next byte djnz r7,start ;read next group stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 427 www.stcmcu.com (2) using 74hc164 expand parallel output ports please refer to the following circuit which using 2 pcs 74hc164 to expand 16 output i/os q a q b q c q d q e q f q g q h 12 11 10 6 5 4 3 a,b clr cp 1,2 9 8 74hc164 12cxx p3.0 p3.1 p1.0 104 13 vcc gnd 7 14 q a q b q c q d q e q f q g q h 12 11 10 6 5 4 3 a,b clr cp 1,2 9 8 74hc164 104 13 vcc gnd 7 14 when serial port is working in mode0, the serial data is input/output from rxd(p3.0) pin and serial clock is output from txd(p3.1). serial data is always starting transmission from the lowest bit. start: mov r7,#02h ;output 2 bytes data mov r0,#30h ;set buffer address mov scon,#00000000b ;set serial as mode 0 send: mov a,@r0 ;read data from buffer mov sbuf,a ;start send data wait: jnb ti,wait ;wait for send complete clr ti ;clear send complete flag inc r0 ;modify buffer ptr djnz r7,send ;send next data stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 428 www.stcmcu.com appendix f: use stc mcu common i/o driving lcd display 7 8 9 10 11 12 13 14 15 16 17 39 38 37 36 35 34 33 32 31 30 29 6 5 4 3 3 1 44 43 42 41 40 18 19 20 21 22 23 24 25 26 27 28 p1.5 p1.6 p1.7 rst p3.0 p4.3 p3.1 p3.2 p3.3 p3.4 p3.5 p0.4 p0.5 p0.6 p0.7 ea p4.1 ale psen p2.7 p2.6 p2.5 p3.6 p3.7 xtal2 xtal1 vss p4.0 p2.0 p2.1 p2.1 p2.3 p2.4 p1.4 p1.3 p1.2 p1.1 p1.0 p4.2 vdd p0.0 p0.1 p0.2 p0.3 8051 seg13 seg14 seg15 vcc c1 10f r1 10k 5.6k r2 5.6k r3 5.6k r4 5.6k r5 5.6k r6 5.6k r7 com0 com1 com2 seg16 seg17 seg18 seg19 seg20 <33pf <33pf seg23 seg22 seg21 vcc seg12 seg11 seg10 seg9 seg8 seg0 seg1 seg2 seg3 vcc seg4 seg5 seg6 seg7 21k 0 com0 u2 com0 1 com1 2 seg0 3 seg1 4 seg2 5 seg3 6 seg4 7 seg5 8 seg6 9 seg7 10 seg8 seg9 seg10 seg11 seg12 seg13 seg14 seg15 seg16 seg17 seg18 seg19 seg20 seg21 seg22 seg23 com2 com1 seg0 seg1 seg2 seg3 seg4 seg5 seg6 seg7 seg8 seg9 seg10 seg11 seg12 seg13 seg14 seg15 seg16 seg17 seg18 seg19 seg20 seg21 seg22 seg23 com2 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 429 www.stcmcu.com name lcddriver #include ;******************************************************************************** ;the lcd is 1/3 duty and 1/3 bias; 3com*24seg; 9 display ram; ; ; bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0 ;com0: com0data0: seg7 seg6 seg5 seg4 seg3 seg2 seg1 seg0 ; com0data1: seg15 seg14 seg13 seg12 seg11 seg10 seg9 seg8 ; com0data2: seg23 seg22 seg21 seg20 seg19 seg18 seg17 seg16 ;com1: com1data0: seg7 seg6 seg5 seg4 seg3 seg2 seg1 seg0 ; com1data1: seg15 seg14 seg13 seg12 seg11 seg10 seg9 seg8 ; com1data2: seg23 seg22 seg21 seg20 seg19 seg18 seg17 seg16 ;com2: com2data0: seg7 seg6 seg5 seg4 seg3 seg2 seg1 seg0 ; com2data1: seg15 seg14 seg13 seg12 seg11 seg10 seg9 seg8 ; com2data2: seg23 seg22 seg21 seg20 seg19 seg18 seg17 seg16 ;******************************************************************************** ;com0: p3^0,p3^1 when p3^0 = p3^1 = 1 then com0=vcc(=5v); ; p3^0 = p3^1 = 0 then com0=gnd(=0v); ; p3^0 = 1, p3^1=0 then com0=1/2 vcc; ;com1: p3^2,p3^3 the same as the com0 ;com2: p3^4,p3^5 the same as the com0 ; sbit seg0 =p0^0 sbit seg1 =p0^1 sbit seg2 =p0^2 sbit seg3 =p0^3 sbit seg4 =p0^4 sbit seg5 =p0^5 sbit seg6 =p0^6 sbit seg7 =p0^7 sbit seg8 =p1^0 sbit seg9 =p1^1 sbit seg10 =p1^2 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 430 www.stcmcu.com sbit seg11 =p1^3 sbit seg12 =p1^4 sbit seg13 =p1^5 sbit seg14 =p1^6 sbit seg15 =p1^7 sbit seg16 =p2^0 sbit seg17 =p2^1 sbit seg18 =p2^2 sbit seg19 =p2^3 sbit seg20 =p2^4 sbit seg21 =p2^5 sbit seg22 =p2^6 sbit seg23 =p2^7 ;********************************************************************************* ;======interrupt=============================== cseg at 0000h ljmp start cseg at 000bh ljmp int_t0 ;======register=============================== lcdd_bit segment bit rseg lcdd_bit outflag: dbit 1 ;the output display reverse flag lcdd_data segment data rseg lcdd_data com0data0: ds 1 com0data1: ds 1 com0data2: ds 1 com1data0: ds 1 com1data1: ds 1 com1data2: ds 1 com2data0: ds 1 com2data1: ds 1 com2data2: ds 1 times: ds 1 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 431 www.stcmcu.com ;======interrupt code========================== t0_int segment code rseg t0_int using 1 ;***************************************************************** ;time0 interrupt ;ths system crystalloid is 22.1184mhz ;the time to get the time0 interrupr is 2.5ms ;the whole duty is 2.5ms*6=15ms, including reverse ;***************************************************************** int_t0: orl tl0,#00h mov th0,#0eeh push acc push psw mov psw,#08h acall outdata pop psw pop acc reti ;======sub code================================ uart_sub segment code rseg uart_sub using 0 ;****************************************************************** ;initial the display ram data ;if want to display other,then you may add other data to this ram ;com0: com0data0,com0data1,com0data2 ;com1: com1data0,com1data1,com1data2 ;com2: com2data0,com0data1,com0data2 ;******************************************************************* initcomdata: ;it will display "11111111" mov com0data0, #24h mov com0data1, #49h mov com0data2, #92h stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 432 www.stcmcu.com mov com1data0, #92h mov com1data1, #24h mov com1data2, #49h mov com2data0, #00h mov com2data1, #00h mov com2data2, #00h ret ;******************************************************************** ;reverse the display data ;******************************************************************** retcomdata: mov r0, #com0data0 ;get the first data address mov r7, #9 retcom_0: mov a, @r0 cpl a mov @r0, a inc r0 djnz r7, retcom_0 ret ;********************************************************************** ;get the display data and send to output register ;********************************************************************** outdata: inc times mov a, times mov p3, #11010101b ;clear display,all com are 1/2vcc and invalidate cjne a, #01h, outdata_1 ;judge the duty mov p0, com0data0 mov p1, com0data1 mov p2, com0data2 jnb outflag,outdata_00 mov p3, #11010111b ;com0 is work and is vcc ret stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 433 www.stcmcu.com outdata_00: mov p3, #11010100b ;com0 is work and is gnd ret outdata_1: cjne a, #02h,outdata_2 mov p0, com1data0 mov p1, com1data1 mov p2, com1data2 jnb outflag,outdata_10 mov p3, #11011101b ;com1 is work and is vcc ret outdata_10: mov p3, #11010001b ;com1 is work and is gnd ret outdata_2: mov p0, com2data0 mov p1, com2data1 mov p2, com2data2 jnb outflag,outdata_20 mov p3, #11110101b ;com2 is work and is vcc sjmp outdata_21 outdata_20: mov p3,#11000101b ;com2 is work and is gnd outdata_21: mov times, #00h acall retcomdata cpl outflag ret ;======main code=============================== uart_main segment code rseg uart_main using 0 stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 434 www.stcmcu.com start: mov sp,#40h clr outflag mov times,#00h mov tl0,#00h mov th0,#0eeh mov tmod,#01h mov ie,#82h acall initcomdata setb tr0 main: nop sjmp main end stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 435 www.stcmcu.com appendix g: led driven by an i/o port and key scan vcc 10k 1k 1k p1.7 vcc 10k 1k 1k p1.6 it can save a lot of i/o ports that stc12c5a60s2 mcu i/o ports can used as the led drivers and key detection concurrently because of their feature which they can be set to the weak pull , the strong pull (push-pull) output, only input (high impedance), open drain four modes. when driving the led, the i/o port should be set as strongly push-pull output, and the led will be lighted when the output is high. when testing the keys, the i/o port should be set as weak pull input, and then reading the status of external ports can test the keys. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 436 www.stcmcu.com appendix h: how to reduce the length of code using keil c 1. choose the "options for target" in "project" menu 2. choose the option "c51" in "options for target" setting as shown below in keil c can maximum reduce about 10k to the length of original code 3. code optimization, 9 common block subroutines 4. click "ok", compile the program once again. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 437 www.stcmcu.com appendix i: notes of stc12 series replaced traditional 8051 stc12c5axx series mcu timer0/timer1/uart is fully compatible with the traditional 8051 mcu. after power on reset, the default input clock source is the divider 12 of system clock frequency, and uart baudrate generator is timer 1. add an independent baud rate generator, saved the timer2 in 8052 system. mcu instruction execution speed is faster than the traditional 8051 mcu 8 ~ 12 times in the same working environment,so software delay programs need to be adjusted. ale traditional 8051's ale pin output signal on divide 6 the system clock frequency can be externally provided clock, if disable ale output in stc12c5axx series system, you can get clock source from clkout0/p3.4, clkout1/p3.5, clkout2/p1.0 or xtal2 clock output. (recommended a 200ohm series resistor to the xtal2 pin). ale pin is an disturbance source when traditional 8051's system clock frequency is too high. stc89xx series mcu add aleofff bit in auxr register. while stc12c5axx series mcu directly disable ale pin dividing 6 the system clock output, and can remove ale disturbance thoroughly. please compare the following two registers. auxr register of stc89xx series mnemonic add name bit7 bit6 bit5 bit4 bir3 bit2 bit1 bit0 reset value auxr 8eh auxiliary register 0 - - - - - - extram aleoff xxxx,xx00 auxr register of stc12c5a60s2 series mnemonic add name bit7 bit6 bit5 bit4 bir3 bit2 bit1 bit0 reset value auxr 8eh auxiliary register t0x12 t1x12 uart_m0x6 brtr s2smod2 brtx12 extram s1brs 0000,0000 psen traditional 8051 execute external program through the psen signal, stc12c5a60s2 series is system mcu concept, integrated high-capacity internal program memory, do not need external program memory expansion generally, so have no psen signal, psen pin can be used as gpio. general qusi-bidirectional i/o traditional 8051 access i/o (signal transition or read status) timing is 12 clocks, stc12c5a60s2 series mcu is 4 clocks. when you need to read an external signal, if internal output a rising edge signal, for the traditional 8051, this process is 12 clocks, you can read at once, but for stc12c5a60s2 series mcu, this process is 4 clocks, when internal instructions is complete but external signal is not ready, so you must delay 1~2 nop operation. p4 port stc12c5a60s2 series mcu has integral p4 port (p4.0~p4.7), and location at address c0h. no extended external interrupt int2/int3. stc12c5a60s2 series is difference from stc89 series (stc89 series mcu has half byte p4 port (p4.0~p4.3), location at addrss e8h, extended external interrupt int2/int3). port drive capability stc12c5a60s2 series i/o port sink drive current is 20ma, has a strong drive capability, the port is not burn out when drive high current generally. stc89 series i/o port sink drive current is only 6ma, is not enough to drive high current. for the high current drive applications, it is strongly recommended to use stc12c5a60s2 series mcu. stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:086-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 438 www.stcmcu.com watchdog stc12c5a60s2 series mcus watch dog timer control register (wdt_contr) is location at c1h, add watch dog reset flag. stc12c5a60s2 series wdt_contr ( c1h ) mnemonic add name bit7 bit6 bit5 bit4 bir3 bit2 bit1 bit0 reset value wdt_contr c1h wact-dog- timer control register wdt_flag - en_wdt clr_wdt idl_wdt ps2 ps1 ps0 xx00,0000 stc89 series wdt_contr ( e1h ) mnemonic add name bit7 bit6 bit5 bit4 bir3 bit2 bit1 bit0 reset value wdt_contr e1h wact-dog-timer control register - - en_wdt clr_wdt idl_wdt ps2 ps1 ps0 xx00,0000 stc12c5a60s2 series mcu auto enable watch dog timer after isp upgrade, but not in stc89 series, so stc12c5a60s2 seriess watch dog is more reliable. eeprom sfr associated with eeprom mnemonic stc12cxx stc89xx description address iap_data c2h e2h isp/iap flash data register iap_addrh c3h e3g isp/iap flash high address register iap_addrl c4h e4h isp/iap flash low address register iap_cmd c5h e5h isp/iap flash command register iap_trig c6h e6h isp/iap command trigger register iap_contr c7h e7h isp/iap control register stc12c5a60s2 series write 5ah and a5h sequential to trigger eeprom flash command, and stc89 series write 46h and b9h sequential to trigger eeprom flash command. stc12c5a60s2 series eeprom start address all location at 0000h, but stc89 series is not. internal/external clock source stc12c5a60s2 series mcu has a optional internal rc oscillator, generally, for 40/44 pin package mcu, set to use external crystal oscillator and for 20/18/16 pin package set to use internl rc oscillator in factory. when use isp download program, user can arbitrarily choose internal rc oscilator or external crystal oscillator. stc89 series mcu can only choose external crystal oscillator. power consumption power consumption consists of two parts: crystal oscillator amplifier circuits and digital circuits. for crystal oscillator amplifier circuits, stc12c5a60s2 series is lower then stc89 series. for digital circuits, the higher clock frequency, the greater the power consumption. stc12c5a60s2 series mcu instruction execution speed is faster than thestc89 series mcu 3~24 times in the same working environment, so if you need to achieve the same efficiency, stc12c5a60s2 series required frequency is lower than stc89 series mcu. powerdown wakeup stc12c5axx series mcu wake-up support for low level or falling edge depend on the external interrupt mode, but stc89 series only support for low level. in addition, stc12c5axx series have a optional power- down wake-up delay length : 32768 / 16384 / 8192 / 4096 clocks . stc mcu limited. free datasheet http:///
mobile:(86)13922809991 tel:86-755-82948412 fax:86-755-82905966 stc mcu limited. website www.stcmcu.com 439 www.stcmcu.com about reset circuit if the system frequency is below 12mhz, the external reset circuit is not required. reset pin can be connected to ground through the 1k resistor or can be connected directly to ground. the proposal to create pcb to retain rc reset circuit about clock oscillator if you need to use internal rc oscillator, xtal1 pin and xtal2 pin must be floating. if you use a external active crystal oscillator, clock signal input from xtal1 pin and xtal2 pin floating. about power power at both ends need to add a 47uf electrolytic capacitor and a 0.1uf capacitor, to remove the coupling and filtering. stc mcu limited. free datasheet http:///


▲Up To Search▲   

 
Price & Availability of STC12LE5204

All Rights Reserved © IC-ON-LINE 2003 - 2022  

[Add Bookmark] [Contact Us] [Link exchange] [Privacy policy]
Mirror Sites :  [www.datasheet.hk]   [www.maxim4u.com]  [www.ic-on-line.cn] [www.ic-on-line.com] [www.ic-on-line.net] [www.alldatasheet.com.cn] [www.gdcy.com]  [www.gdcy.net]


 . . . . .
  We use cookies to deliver the best possible web experience and assist with our advertising efforts. By continuing to use this site, you consent to the use of cookies. For more information on cookies, please take a look at our Privacy Policy. X